Articles → Android → Create A Sqlite Database In Android
Create A Sqlite Database In Android
Database Creation
- Create a new class inherited from SQLiteOpenHelper class. Here is the code
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) {}
}
- Create the instance of DBHelper class in background process.
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) {}
}
- In the main activity, execute the background worker process.
BackgroundProcess process = new BackgroundProcess(this);
process.execute();
- Finally add the permission in assemblymanifest.xml
<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) });