How to store your data in your SQLite database on your android mobile device using Java

M.Khalil ANEBDOUR
3 min readNov 25, 2019

--

source: https://www.pexels.com/photo/white-apple-iphone-on-wooden-table-48605/

What is SQLite:

SQLite is a open source project and it’s type of database management system for mobile devices in the form of a library written in C language that offers a relational database engine accessible using SQL language.

How to use it?:

SQLite is built into every Android device. Using an Android SQLite database doesn’t require any configuration or administration of the database.

let’s make this article quick i’m going to use android studio IDE:

First thing to do is create a java class that content all methods and parameters to create a connection with your sqlite system.

Go to your package work in your app/java folder and create a class in this example i’m going to name it SQLiteHelper,

this class (SQLiteHelper) should extends from SQLiteOpenHelper witch is a abstract class so that we should implement some method, this class is your helper to manage database creation and version management.

next step is to implement a constructor for this class and some method.

public class SQLiteHelper extends SQLiteOpenHelper {

public static String dbname = "YourDbName.db";
/*
this is the constuctor, we can give to the factory null and version 1
*/
public SQLiteHelper(@Nullable Context context) {
super(context, dbname, null, 1);
}
/*
onCreate and onUpgrade are two method that we implement from the abstract class
*/
@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

}

until now your database management system is ready.

Now we’re going to make the body of these method:

onCreate method

public void onCreate(SQLiteDatabase db) {
db.execSQL("create table YourDbName( id INTEGER PRIMARY KEY AUTOINCREMENT, info TEXT )");
}

this method is for creating the table in the sqlite system

onUpgrade method

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS YourDbName");
onCreate(db);
}

this method destroy the table if it exist then it create a new one.

new some method to add and show your data:

public boolean addData(String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put("info", name);

long result = db.insert("infoNetwork", null, contentValues);

return result != -1;
}

first of all we create SQLiteDatabase variable and we instantiate it with this.getWritableDatabase() witch means SQLiteHelper.getWritableDatabase()

if every thing is done correctly we return true.

we get the object of table that we can write in.

public ArrayList<String> getdata(){
ArrayList<String> listData = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();

Cursor res = db.rawQuery("SELECT * FROM infoNetwork ", null);

res.moveToFirst();

while (!res.isAfterLast()){
String t1 = res.getString(0);
String t2 = res.getString(1);

listData.add(t1+" - "+t2);

res.moveToNext();
}

return listData;
}

in this method we did the same thing this time with getReadableDatabase,

tables that we can read then we insert this result in an ArrayList that we will insert in ViewList next.

This is interaction part of the game and all that hard part is done here.

all we need to do is where ever we want to use our SQLite we just need to call and instantiate the class that we made and use all methods, in my case:

SQLiteHelper db = new SQLiteHelper(this);

here is complete code example to see in my github here for this tutoriel.

--

--

M.Khalil ANEBDOUR

Financial market R&D engineer, Eager to learn new things. ✌🏼