Articles → Android → Create A Sqlite Database In Android

Create A Sqlite Database In Android






Database Creation




  1. Create a new class inherited from SQLiteOpenHelper class. Here is the code
  2. package com.example.demo_application;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import java.io.File;
    
    public class DBHelper extends SQLiteOpenHelper {
      private static final int DB_VERSION = 1;
    
      private static final String DB_NAME = "database.db";
    
      public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        File file = context.getDatabasePath(DB_NAME);
        SQLiteDatabase.openOrCreateDatabase(file.getPath(), null);
      }
    
      @Override
      public void onCreate(SQLiteDatabase sqLiteDatabase) {}
    
      @Override
      public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {}
    }


  3. Create the instance of DBHelper class in background process.
  4. package com.example.demo_application;
    import android.content.Context;
    import android.os.AsyncTask;
    
    public class BackgroundProcess extends AsyncTask <String,
    String,
    String> {
      Context ctx;
      public BackgroundProcess(Context ctx) {
        this.ctx = ctx;
      }
    
      @Override
      protected void onPreExecute() {
        super.onPreExecute();
      }@Override
      protected String doInBackground(String...strings) {
        DBHelper db = new DBHelper(ctx);
        return "";
      }@Override
      protected void onPostExecute(String s) {}
    
      @Override
      protected void onProgressUpdate(String...values) {}
    }


  5. In the main activity, execute the background worker process.
  6. BackgroundProcess process = new BackgroundProcess(this);
    process.execute();


  7. Finally add the permission in assemblymanifest.xml
<uses-permission    
              android:name="android.permission.WRITE_EXTERNAL_STORAGE"    
              android:maxSdkVersion="29" />




Picture showing the sqlite database added inside the databases folder
Click to Enlarge


How To Create Table?




SQLiteDatabase sQLiteDatabase =  SQLiteDatabase.openOrCreateDatabase(file.getPath(), null);

sQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS country (id INTEGER CONSTRAINT country_pk_id PRIMARY KEY AUTOINCREMENT NOT NULL, country_name VARCHAR (100) UNIQUE COLLATE NOCASE NOT NULL);");




SQLiteDatabase sQLiteDatabase =  SQLiteDatabase.openOrCreateDatabase(file.getPath(), null);
sQLiteDatabase.execSQL("Insert Into Country (country_name) values('India')");



Select Query




Cursor c = sQLiteDatabase.rawQuery("SELECT * FROM country", null);
if (c.moveToFirst()) {
  do {
    // Passing values        
    Log.v("Country Id", c.getString(0));
    Log.v("Country Name", c.getString(1));
    // Do something Here with values    
  } while ( c . moveToNext ());
}




Picture showing the output of the select query in sqlite database in android application
Click to Enlarge


Inserting Data In Table Using Contentvalues




ContentValues values = new ContentValues();
values.put("country_name", "India");
sQLiteDatabase.insert("country", null, values);





Update Data In Table Using Contentvalues




ContentValues values = new ContentValues();
values.put("country_name", "Australia");
sQLiteDatabase.update("country", values, "id = ?", new String[] { Long.toString(1) });



Deleting Data Using Delete Method




sQLiteDatabase.delete("country", "id = ?", new String[] { Long.toString(1) });



Posted By  -  Karan Gupta
 
Posted On  -  Monday, November 11, 2019
 
Updated On  -  Friday, February 14, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250