top of page
Search
  • stephie.john85

How to Store Mobile App Data Offline using SQLite



It’s true that you can’t compare SQLite with My SQL or Oracle due to the nature of work being entirely different but you should really know how SQLite can make a difference. But before that, if you don’t know How to Store Mobile App Data Offline using SQLite yet LIA infraservices the leading mobile app development company in Chennai has done several local data storage for individual applications and devices.



Now I’ll tell you what’s SQLite:

A Serverless architecture that can access the database regardless of the storage. It comes with zero configuration and is transactional too. Since it requires minimal assistance from the operating system it’s self-contained as well.

Key Features of Using SQLite:

  • Structured query DB

  • Light weight

  • No internet

  • Access multiple databases simultaneously.

SQLite Database Methods in Android Mobile Application:

  • · Insert

  • · Update

  • · Delete

  • · Query (view data)

Why SQLite?

1. Mobile Application needs to store a large amount of data

2. You need not have a separate server to store your data nor access them. The key highlight of SQLite is that it’s integrated with the application.

3. SQLite Is an open-source relational database i.e. used to perform database operations on android devices such as storing, manipulating or retrieving persistent data from the database.

4. It will be helpful to reduce the time delay and keep the process alive in a Mobile application.

Contact Lia Infraservices the No.1 Mobile Application Development Company in Chennai for enterprise-grade secured mobile apps.

How to View and Locate SQLite Database in Android Studio?

  1. Connect device and Device file explorer can be found in the bottom-right corner of the android studio screen.



DB directory:

  1. To search your package name go to data > data> package name. Click on package name.

data/data/package_name/databases/DATABASE_NAME




  1. Download the database (snehadb )

Right-click –>save as

How to view databases?

1. To view the database we required SQLite browser

Download link: https://sqlitebrowser.org/dl/. (image 1)

2. In DB browser, go to Open database→ select downloaded db file→ browse data→ select table name(userdatas)(image 2)





7 steps to store Mobile App Data Offline using SQLite:

1. Create database

For creating database, create java class, DbHelper

public class DbHelper extends SQLiteOpenHelper {}

It will create 2 methods,

  • onCreate

  • onUpgrade

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_NEWUSER_TABLE);

}

@Override

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

db.execSQL(“DROP TABLE IF EXISTS ” + TABLE_NAME);

onCreate(db);

}

2. Create static variables for database table

public static final String DATABASE_NAME = “snehadb”;

public static final String TABLE_NAME = “users”;

public static final String NAME = “name”;(col 1)

public static final String SYNC_STATUS = “syncstatus”;(col 2)

public static final String USER_COL_ID = “id”;(col 3)

3. Create constructor

public DbHelper( Context context) {

super(context,DATABASE_NAME, null, 1);

}

4. Multiple cols in table syntax:

String CREATE_NEWUSER_TABLE = “CREATE TABLE ” + TABLE_NAME + ” (“

+ USER_COL_ID + ” INTEGER PRIMARY KEY AUTOINCREMENT, ” + NAME +

” TEXT, ” + SYNC_STATUS + ” INTEGER” + “)”;

5. To save in local Database, need ContentValues

public void LocalDatabase(String name,SQLiteDatabase database){

ContentValues contentValues=new ContentValues();

contentValues.put(NAME,name);

database.insert(TABLE_NAME,null,contentValues);

}

6. To read data from database, need cursor object

public Cursor readLocalDatabase(SQLiteDatabase database){

String[]projection={NAME};

return (database.query(TABLE_NAME,projection,null,

null,null,null,

null));}

7. In main activity initialize

DbHelper dbHelper=new DbHelper(this);

SQLiteDatabase database=dbHelper.getReadableDatabase();

And use save , read methods from the DbHelper class when it is needed.

private void readLocalStorage(){

arrayList.clear();

DbHelper dbHelper=new DbHelper(this);

SQLiteDatabase database=dbHelper.getReadableDatabase();

Cursor cursor=dbHelper.readFromLocalDatabase(database);

while (cursor.moveToNext()){

String name=cursor.getString(cursor.getColumnIndex(DbHelper.NAME));

arrayList.add(new Contact(name,sync_status));

}

adapter.notifyDataSetChanged();

cursor.close();

dbHelper.close();

}

API :

public static String url=“https://liastaging.com/soluk/customer/addpeople”;

public static String urlget=“https://liastaging.com/soluk/customer/listpeople”;

Errors I faced:

1. No such column in database

In constructor, need to mention sqlite version, here i mentioned as 1

public DbHelper( Context context) {

super(context,DATABASE_NAME, null, 1);

}

Then I changed to version 2 and changed some static variables values like the name of the database and table name.

The error occurs because the database was created already with old version 1 and old table names so override was not supported there.

Solution:

Clear cache data or uninstall the app and run again to create a new database.

I correct the syntax for creating col name

String CREATE_NEWUSER_TABLE = “CREATE TABLE ” + TABLE_NAME + ” (“

+ USER_COL_ID + ” INTEGER PRIMARY KEY AUTOINCREMENT, ” + NAME +

” TEXT, ” + SYNC_STATUS + ” INTEGER” + “)”;

In recycler view UI , the text was not displayed

In the model class’s constructor,

I generate a default constructor

public Contact(String name, int sync_status) {

Name = name;

Sync_status = sync_status;

}

But I declare the String variable as private String Name;

In the default constructor, it was a String name.

Solution:

public Contact(String Name, int Sync_status) {

this.setName(Name):}

Conclusion:


If you still wonder if SQL is the better solution it requires less maintenance and it’s more reliable as well. If your device is local storage that has low concurrency then needn’t wink.

Do you have an idea that needs to be built in the way you look for? Rush not, take a moment to peek at our works by a mobile app development company in Chennai. Lia infraservices has been a stand-alone iOS App Development Company in Chennai as well as an Android App Development company in Chennai.


20 views0 comments
bottom of page