如何使用本机代码访问SQLite数据库?

时间:2018-06-30 08:42:58

标签: android flutter

我已经使用sqflite插件快速创建了数据库。我在android native中为我的应用创建了android主屏幕小部件。现在,我需要从SQLite获取数据。那就是我试图使用Android Native sqflite Package从SQLite数据库中获取数据。有可能吗?

2 个答案:

答案 0 :(得分:2)

请参阅此post

 List<String> r = new ArrayList<String>();

这属于onCreate类:

 new LongOperation().execute("");

然后您可以创建一个新类:

 private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
             connect();
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
          ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
         ListView list=(ListView)findViewById(R.id.listView1);
         list.setAdapter(adapter);
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }

答案 1 :(得分:0)

>波动没有限制

Sqlite的基本规则,您需要记住

  1. sqflite可用于SQLite数据库之上
  2. 因此,当flutter插件创建数据库时,它可用于本机代码(验证文件夹结构)
  3. 您可以通过在Native Android或IOS上提供SQLiteDatabase的数据库路径来打开数据库
  4. 最后,您可以通过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 yepi1@yep.com
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