Articles → Android → Create a sqlite database in android

Create a sqlite database in android






Database creation






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) {}
}




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) {}
}




BackgroundProcess process = new BackgroundProcess(this);
process.execute();




<uses-permission    
              android:name="android.permission.WRITE_EXTERNAL_STORAGE"    
              android:maxSdkVersion="29" />





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 ());
}





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