Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access SQLite database in Native code?

I have created the database in flutter using sqflite plugin. I created android home screen widgets for my app in android native. Now I need to fetch the data from SQLite. That is I am trying to fetch the data using Android Native sqflite Package from the SQLite db. Is that possible?

like image 225
Dineshkumar Avatar asked Jun 30 '18 08:06

Dineshkumar


Video Answer


1 Answers

> There is No Limit on Flutter

Basic Rules of Sqlite, you need to remember

  1. sqflite is works on Top of SQLite Database
  2. so , when ever flutter plugin creates the database ,it was availible for native code (verify folder structure)
  3. You can OpenDatabase by giving Database Path to SQLiteDatabase on Native Android or IOS
  4. finally you can Read table data by query()

enter image description here

    private fun accessDB(){

    try {
        GlobalScope.launch {

            var db:SQLiteDatabase = SQLiteDatabase.openDatabase(getDatabasePath("epistle.db").absolutePath,null,SQLiteDatabase.OPEN_READWRITE);

            Log.d(MainActivity::class.java.name , "Database Path "+db.path.toString())
            Log.d("" , "Database version "+db.version.toString())

            var cursor = db.query("userInfo",null,null,null,null,null,null)

            cursor?.let {
                while (cursor.moveToNext()){

                    var columnNames =cursor.columnNames

                    columnNames.forEach {
                        Log.d(
                                MainActivity::class.java.name,
                                "$it "+cursor.getString(cursor.getColumnIndex(it))
                        )
                    }

                }
            }

        }

    }catch (e:Exception){
        e.printStackTrace();
    }
}

MainActivity: Database Path /data/u
MainActivity: id 1
MainActivity: uid XfXCx613bKWl0EuDU
MainActivity: username Lee And May
MainActivity: email [email protected]
MainActivity: location US
MainActivity: isBiometricEnabled 0
MainActivity: isNotificationsEnable
MainActivity: morning 152.549
MainActivity: evening 166.529
MainActivity: plan Basic
MainActivity: lattitude 0
MainActivity: lognitude 0
MainActivity: userID XfXCx613bKWl0E
MainActivity: theme 2
MainActivity: premiumStartDate null
MainActivity: premiumEndDate null
MainActivity: isPremium 0
like image 80
Mallikarjuna Avatar answered Nov 09 '22 15:11

Mallikarjuna