Skip to main content

Android SQLite Database Operations

MainActivity.java

package com.firstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
Button Login, Register, Delete, Update;
int status = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Login = (Button) findViewById(R.id.Login);
        Register = (Button) findViewById(R.id.Reg);
        Delete = (Button) findViewById(R.id.Delete);
        Update = (Button) findViewById(R.id.Update);
        Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
status = 1;
Bundle b = new Bundle();
b.putInt("status", status);
Intent i  = new Intent("login_filter");
i.putExtras(b);
startActivity(i);
}
});
        
        Register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i  = new Intent("register_filter");
startActivity(i);
}
});
        Update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
status = 2;
Bundle b = new Bundle();
b.putInt("status", status);
Intent i  = new Intent("login_filter");
i.putExtras(b);
startActivity(i);
}
});
        Delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
status = 3;
Bundle b = new Bundle();
b.putInt("status", status);
Intent i  = new Intent("login_filter");
i.putExtras(b);
startActivity(i);
}
});
    }
}


LoginActivity.java

package com.firstapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
Button Login;
EditText USERNAME,USERPASS;
String username,userpass;
Context CTX = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
Login = (Button) findViewById(R.id.b_login);
USERNAME = (EditText) findViewById(R.id.user_name);
USERPASS = (EditText) findViewById(R.id.user_pass);
Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Bundle b = getIntent().getExtras();
int status = b.getInt("status");
if(status == 1)
{
Toast.makeText(getBaseContext(), "Please wait...", Toast.LENGTH_LONG).show();
username = USERNAME.getText().toString();
userpass = USERPASS.getText().toString();
DatabaseOperations DOP = new DatabaseOperations(CTX);
Cursor CR = DOP.getInformation(DOP);
CR.moveToFirst();
boolean loginstatus = false;
String NAME = "";
do
{
if(username.equals(CR.getString(0))&& (userpass.equals(CR.getString(1))))
{
loginstatus = true;
NAME = CR.getString(0);
}
}while(CR.moveToNext());
if(loginstatus)
{
Toast.makeText(getBaseContext(), "Login Success----\n Welcome "+NAME, Toast.LENGTH_LONG).show();
   finish();
}
else
{
Toast.makeText(getBaseContext(), "Login Failed---- ", Toast.LENGTH_LONG).show();
   finish();
}
}
else if(status == 2)
{
Toast.makeText(getBaseContext(), "Please wait...", Toast.LENGTH_LONG).show();
username = USERNAME.getText().toString();
userpass = USERPASS.getText().toString();
DatabaseOperations DOP = new DatabaseOperations(CTX);
Cursor CR = DOP.getInformation(DOP);
CR.moveToFirst();
boolean loginstatus = false;
String NAME = "";
do
{
if(username.equals(CR.getString(0))&& (userpass.equals(CR.getString(1))))
{
loginstatus = true;
NAME = CR.getString(0);
}
}while(CR.moveToNext());
if(loginstatus)
{
Toast.makeText(getBaseContext(), "Login Success----\n Welcome "+NAME, Toast.LENGTH_LONG).show();
  
Intent i = new Intent("update_filter");
Bundle BN = new Bundle();
BN.putString("user_name",NAME );
BN.putString("user_pass",userpass );
i.putExtras(BN);
startActivity(i);
finish();
}
else
{
Toast.makeText(getBaseContext(), "Login Failed---- ", Toast.LENGTH_LONG).show();
   finish();
}
}
else if(status == 3)
{
Toast.makeText(getBaseContext(), "Please wait...", Toast.LENGTH_LONG).show();
username = USERNAME.getText().toString();
userpass = USERPASS.getText().toString();
DatabaseOperations DOP = new DatabaseOperations(CTX);
Cursor CR = DOP.getInformation(DOP);
CR.moveToFirst();
boolean loginstatus = false;
String NAME = "";
do
{
if(username.equals(CR.getString(0))&& (userpass.equals(CR.getString(1))))
{
loginstatus = true;
NAME = CR.getString(0);
}
}while(CR.moveToNext());
if(loginstatus)
{
Toast.makeText(getBaseContext(), "Login Success----\n Welcome "+NAME, Toast.LENGTH_LONG).show();
Intent i = new Intent("delete_filter");
Bundle B = new Bundle();
B.putString("user_name",NAME );
i.putExtras(B);
startActivity(i);
finish();
}
else
{
Toast.makeText(getBaseContext(), "Login Failed---- ", Toast.LENGTH_LONG).show();
   finish();
}
//Intent i = new Intent("delete_filter");
//startActivity(i);
}
}
});
}

}


RegisterActivity.java

package com.firstapp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {
EditText USER_NAME,USER_PASS,CON_PASS;
String user_name,user_pass,con_pass;
Button REG;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register_layout);
USER_NAME = (EditText) findViewById(R.id.reg_user);
USER_PASS = (EditText) findViewById(R.id.reg_pass) ;
CON_PASS = (EditText) findViewById(R.id.con_pass);
REG = (Button) findViewById(R.id.user_reg);
REG.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
user_name = USER_NAME.getText().toString();
user_pass = USER_PASS.getText().toString();
con_pass = CON_PASS.getText().toString();
if(!(user_pass.equals(con_pass)))
{
Toast.makeText(getBaseContext(),"Passwords are not matching", Toast.LENGTH_LONG).show();
   USER_NAME.setText("");
   USER_PASS.setText("");
   CON_PASS.setText("");
}
else
{
DatabaseOperations DB = new DatabaseOperations(ctx);
DB.putInformation(DB, user_name, user_pass);
Toast.makeText(getBaseContext(), "Registration success", Toast.LENGTH_LONG).show();
finish();
}
}
});
}

}

DeleteActivity.java

package com.firstapp;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DeleteActivity extends Activity {
Bundle bn;
String USERNAME;
Button Del;
EditText PASS;
String Pass;
DatabaseOperations DOP;
Context CTX = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.delete_layout);
Del = (Button) findViewById(R.id.b_delete);
PASS = (EditText) findViewById(R.id.del_pass);
bn = getIntent().getExtras();
USERNAME = bn.getString("user_name");
Del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Pass = PASS.getText().toString();
DOP = new DatabaseOperations(CTX);
Cursor CR = DOP.getUserPass(DOP, USERNAME);
CR.moveToFirst();
boolean login_status = false;
do
{
if(Pass.equals(CR.getString(0)))
{
login_status = true;
}
}while(CR.moveToNext());
if(login_status)
{
DOP.deleteUser(DOP,USERNAME , Pass);
Toast.makeText(getBaseContext(), "User Removed successfully.....", Toast.LENGTH_LONG).show();
finish();
}
else
{
Toast.makeText(getBaseContext(), "Invalid user.....Try later", Toast.LENGTH_LONG).show();
   finish();
}
}
});
}

}


UpdateActivity.java

package com.firstapp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UpdateActivity extends Activity {
String user_name,user_pass,New_user_name;
Button b_update;
EditText newuser;
Context CTX = this;
DatabaseOperations DOP;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.update_layout);
Bundle BN = getIntent().getExtras();
user_name = BN.getString("user_name");
user_pass = BN.getString("user_pass");
b_update = (Button) findViewById(R.id.b_update);
newuser = (EditText) findViewById(R.id.new_user_name);
b_update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
New_user_name = newuser.getText().toString();
DOP = new DatabaseOperations(CTX);
DOP.updateUserInfo(DOP, user_name, user_pass, New_user_name);
Toast.makeText(getBaseContext(), "Updation Success.....", Toast.LENGTH_LONG).show();
   finish();
}
});
}

}


DatabaseOperations.java

package com.firstapp;

import com.firstapp.TableData.TableInfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.SyncStateContract.Columns;
import android.util.Log;

public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version = 1;
public String CREATE_QUERY = "CREATE TABLE "+TableInfo.TABLE_NAME+"("+TableInfo.USER_NAME+" TEXT,"+TableInfo.USER_PASS+" TEXT);";                              

public DatabaseOperations(Context context) {
super(context, TableInfo.DATABASE_NAME, null, database_version);
Log.d("Database operations", "Database created");
}

@Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(CREATE_QUERY);
Log.d("Database operations", "Table created");

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}
public void putInformation(DatabaseOperations dop,String name,String pass)
{
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableInfo.USER_NAME, name);
cv.put(TableInfo.USER_PASS, pass);
long k = SQ.insert(TableInfo.TABLE_NAME, null, cv);
Log.d("Database operations", "One raw inserted");
}
public Cursor getInformation(DatabaseOperations dop)
{
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] coloumns = {TableInfo.USER_NAME,TableInfo.USER_PASS};
Cursor CR = SQ.query(TableInfo.TABLE_NAME,coloumns, null, null, null, null, null);
return CR;
}
public Cursor getUserPass(DatabaseOperations DOP, String user)
{
   SQLiteDatabase SQ = DOP.getReadableDatabase();
   String selection = TableInfo.USER_NAME +" LIKE ?";
   String coloumns[] = {TableInfo.USER_PASS};
   String args[] = {user};
   Cursor CR = SQ.query(TableInfo.TABLE_NAME, coloumns, selection, args, null, null, null);
return CR;
}
public void deleteUser(DatabaseOperations DOP, String user, String pass)
{
String selection = TableInfo.USER_NAME+ " LIKE ? AND "+TableInfo.USER_PASS +" LIKE ?";
//String coloumns[] = {TableInfo.USER_PASS};
String args[] = {user,pass};
SQLiteDatabase SQ = DOP.getWritableDatabase();
SQ.delete(TableInfo.TABLE_NAME, selection, args);
}
public void updateUserInfo(DatabaseOperations DOP, String user_name, String user_pass, String new_user_name )
{
SQLiteDatabase SQ  = DOP.getWritableDatabase();
String selection = TableInfo.USER_NAME+ " LIKE ? AND "+TableInfo.USER_PASS +" LIKE ?";
String args[] = {user_name,user_pass};
ContentValues values = new ContentValues();
values.put(TableInfo.USER_NAME, new_user_name);
SQ.update(TableInfo.TABLE_NAME, values, selection, args);
}

}

TableData,java

package com.firstapp;

import android.provider.BaseColumns;

public class TableData {
public TableData()
{
}
public static abstract class TableInfo implements BaseColumns
{
public static final String USER_NAME = "user_name" ;
public static final String USER_PASS = "user_pass" ;
public static final String DATABASE_NAME = "userdb97";
public static final String TABLE_NAME = "regtb97";
}

}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.firstapp.MainActivity" >

    <Button
        android:id="@+id/Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Reg"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:text="Login" />

    <Button
        android:id="@+id/Delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Update"
        android:layout_below="@+id/Update"
        android:layout_marginTop="46dp"
        android:text="Delete User" />

    <Button
        android:id="@+id/Reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Update"
        android:layout_below="@+id/Login"
        android:layout_marginTop="23dp"
        android:text="Register" />

    <Button
        android:id="@+id/Update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Reg"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="Update Info" />

</RelativeLayout>

login_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter User name"
         >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/user_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" 
        android:hint="Enter password"
        />

    <Button
        android:id="@+id/b_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login" />

</LinearLayout>

register_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/reg_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter User name"
         >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/reg_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword"
        android:hint="Enter password"
         />

    <EditText
        android:id="@+id/con_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" 
        android:hint="Confirm Password"
        />

    <Button
        android:id="@+id/user_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register" />

</LinearLayout>

delete_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hi User Delete your account from here" />

    <EditText
        android:id="@+id/del_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter your password here"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/b_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Delete Account" />

</LinearLayout>

update_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/infotext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="24dp"
        android:text="hello User Update user name Here" />

    <EditText
        android:id="@+id/new_user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/infotext"
        android:layout_below="@+id/infotext"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:hint="Enter a New User name"
         >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/b_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/new_user_name"
        android:layout_marginTop="20dp"
        android:text="Update " />

</RelativeLayout>















Comments

  1. Intent i = new Intent("xxxx_filter");
    startActivity(i);

    Could you show the Manifest entry for this? Thanks.

    ReplyDelete
  2. hai i want calendar android project pls help me

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Swipe Views with Tabs

In this post we are going to learn about how to integrate the android tab view with the fragments using ViewPager and ActionBar class. For displaying the tabs on the top of the screen you need to interact with the android action bar, this is because the tab views is connected with the action bar. Ads by Google In this example application we make three tabs called "java", "php" and ".Net" and there are three seperate fragement view for each of these tabs. First you need to add the ViewPager into the activity_main.xml file. <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> Now create three layout files for the fragments. 1. java_layout.xml <?xml version="1.0" encod

Android MySQL Database Operations

In this post we are going to learn about how to connect to MySQL Database from your Android Application and perform database operations. Here we create an android app that contain some login Activity through which the user can retrieve information from database and a registration Activity through which the user can add information into the database.  First you need to have the following components installed in your development machine.  1. Database : Here we use the MySQL database. 2. Web Server : Here we use the Apache Web Server. 3. Server side Scripting Language :   Here we use PHP for server side scripting. 4. Android Development environment : You must install android sdk and android studio.   I recommend you to download and install WAMPSERVER. The wamp server installer contains the following components. Apache Server Application MySQL Database PHP/phpMyAdmin First we have to create the database and table in MySQL. You can use the phpMyAdmin for mange yo

"please correct the errors on this form" adsense error simple solution

Many of the bloggers now facing the problem with their adsense widgets. Whenever try to add the new  adsense link unit using the blogger widgets, it shows the error " please correct the errors on this form " as shown bellow. In blogger you can add the adsense units in two ways. First one is adding the adsense using the blogger widgets(Most of the beginners doing this) and the second method is to obtain the adsense code from the adsense login page and place it into the targeted position on the blog. To improve the revenue from adsense you have to place the appropriate ad unit into the right position. The adsense link units are very important for increasing the adsense revenue. So due to this error many of the bloggers are unable to place the link units.  Here is the solution for the problem. 1. Login into your adsense and blogger account. 2. Now make sure that only two adsense widgets (units) are present on your blog, this is because Google allows onl