SQLite Database in Android

Overview

 

SQLite DataBase

 
SQLiteOpenHelper
  1. getWritableDataBase()
  2. getReadableDataBase()
  3. onCreate()
  4. onUpgrade()
Some query methods in SQLiteDataBase:
 
- execSQL()
- Insert()---------------ContentValues----------Put(String key,String value)
- Query()------Cursor------moveToFirst(),moveToNext(),getCount(),moveToPosition()
- update()
- delete()
 
Now there are some commands that need to be written in a Command Prompt to see the entries in the database.
 
The commands are as follows:
  1. set path of the bin
  2. set path of platform-tools
  3. adb shell
  4. cd data
  5. cd package name of the application
  6. cd databases
  7. pwd(present working directory)
  8. ls
  9. SQLite3 (name of database -db)
  10. table
  11. select * from table name;
Here are some snapshots of this.
 
SQLite-in-Android.jpg
 
The procedure of coding:
  1. Make an XML file activity_main.xml with two EditTexts and one Button.
  2. Just look them up in the MainActivity.java file
  3. Use another class named Mydatabase.java
  4. After that follow the commands as given above to see the entries in the database.
Code
 
MainActivity.java
  1. package com.example.sqlite;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.content.ContentValues;  
  5. import android.database.sqlite.SQLiteDatabase;  
  6. import android.database.sqlite.SQLiteOpenHelper;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;   
  13. public class MainActivity extends Activity implements OnClickListener  
  14. {  
  15.         EditText euser, epass;  
  16.         Button inser;  
  17.        Mydatabase o1=new Mydatabase(this,"emp.db",null1);  
  18.       @Override  
  19.       protected void onCreate(Bundle savedInstanceState)  
  20.       {  
  21.   
  22.                super.onCreate(savedInstanceState);  
  23.               setContentView(R.layout.activity_main);                    
  24.               euser=(EditText) findViewById(R.id.editText1);  
  25.               epass=(EditText) findViewById(R.id.editText2);  
  26.               inser=(Button) findViewById(R.id.button1);  
  27.   
  28.             inser.setOnClickListener(new OnClickListener()  
  29.             {  
  30.                    @Override  
  31.             public void onClick(View v)  
  32.             {  
  33.                    // TODO Auto-generated method stub  
  34.                    add();  
  35.              }  
  36.        });  
  37.   }  
  38.      public void add()  
  39.      {                              
  40.             SQLiteDatabase db=o1.getWritableDatabase();  
  41.             ContentValues cv=new ContentValues();  
  42.             cv.put(Mydatabase.user, euser.getText().toString());  
  43.             cv.put(Mydatabase.pass, epass.getText().toString());  
  44.             db.insert(Mydatabase.dtab, null, cv);  
  45.             Toast.makeText(getApplicationContext(), "hii"1000).show();  
  46.      }  
  47.     @Override  
  48.      public void onClick(View v)  
  49.      {  
  50.       // TODO Auto-generated method stub                 
  51.       }  
Mydatabse.java
  1. package com.example.sqlite;  
  2. import android.content.Context;  
  3. import android.database.sqlite.SQLiteDatabase;  
  4. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  5. import android.database.sqlite.SQLiteOpenHelper;  
  6. import android.database.sqlite.*;  
  7.    
  8. public class Mydatabase extends SQLiteOpenHelper  
  9. {           
  10.       //declare dbname  
  11.       public static final String dbname="emp.db";  
  12.       public static final String dtab="emp1";           
  13.       //declare clmn nm           
  14.       public static final String user="user";  
  15.       public static final String pass="pass";  
  16.       public static final int dver=1;  
  17.                               
  18.       public Mydatabase(Context database, String dbname, CursorFactory factory, int dver)  
  19.       {  
  20.                super(database, dbname, factory, dver);  
  21.                // TODO Auto-generated constructor stub  
  22.       }  
  23.      @Override  
  24.      public void onCreate(SQLiteDatabase db)  
  25.      {  
  26.            // TODO Auto-generated method stub                    
  27.            String str="create table emp1(_id integer primary key autoincrement,user text,pass text not null);";  //first ; query termination and later ; is statement termination                    
  28.            db.execSQL(str);                                       
  29.     }  
  30.    @Override  
  31.    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)  
  32.    {  
  33.         // TODO Auto-generated method stub                    
  34.    }  
activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools"  
  3. android:layout_width="match_parent"  
  4. android:layout_height="match_parent"  
  5. android:paddingBottom="@dimen/activity_vertical_margin"  
  6. android:paddingLeft="@dimen/activity_horizontal_margin"  
  7. android:paddingRight="@dimen/activity_horizontal_margin"  
  8. android:paddingTop="@dimen/activity_vertical_margin"  
  9. tools:context=".MainActivity" >  
  10.    
  11.   <Button  
  12.   android:id="@+id/button1"  
  13.   android:hint="press"  
  14.   android:layout_width="wrap_content"  
  15.   android:layout_height="wrap_content"  
  16.   android:layout_alignParentBottom="true"  
  17.   android:layout_centerHorizontal="true"  
  18.   android:layout_marginBottom="102dp"  
  19.   android:text="Button" />  
  20.    
  21.   <EditText  
  22.   android:id="@+id/editText1"  
  23.   android:hint="please enter username"  
  24.   android:layout_width="wrap_content"  
  25.   android:layout_height="wrap_content"  
  26.   android:layout_alignParentTop="true"  
  27.   android:layout_marginTop="44dp"  
  28.   android:ems="10"  
  29.   android:inputType="textPersonName" >  
  30.     <requestFocus />  
  31.   </EditText>  
  32.    
  33.   <EditText  
  34.   android:id="@+id/editText2"  
  35.   android:hint="please enter password"  
  36.   android:layout_width="wrap_content"  
  37.   android:layout_height="wrap_content"  
  38.   android:layout_alignRight="@+id/editText1"  
  39.   android:layout_below="@+id/editText1"  
  40.   android:layout_marginTop="57dp"  
  41.   android:ems="10"  
  42.   android:inputType="textPersonName" />  
  43. </RelativeLayout> 


Similar Articles