Can't access pre populated SQLite database using PhoneGap/Cordova in Android

霸气de小男生 提交于 2019-12-18 07:24:31

问题


We are trying to create a mobile app using PhoneGap/Cordova. We need to ship this app with a pre-populated SQLite db. We are able to copy the DB using below code. But when we try to access tables from copied DB in our App we get 'No Such Table' error. Can anyone help us in identifying the cause ?

We copied the database using the below code to data/data/package_name/databases folder.

   try
    {
        File dbFile = getDatabasePath("Bee_dict.db");
        if(!dbFile.exists()){
            this.copy("Bee_dict.db",dbFile.getAbsolutePath());
        }
     }
     catch (Exception e)
     {
     e.printStackTrace();
     }


//And our copy function:

   void copy(String file, String folder) throws IOException
    {
     File CheckDirectory;
     CheckDirectory = new File(folder);


     String parentPath = CheckDirectory.getParent();

     File filedir = new File(parentPath);
     if (!filedir.exists()) {
         if (!filedir.mkdirs()) {
             return;
         }
     }

        InputStream in = this.getApplicationContext().getAssets().open(file);
        File newfile = new File(folder);
        OutputStream out = new FileOutputStream(newfile);


        byte[] buf = new byte[1024];
        int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
        in.close(); out.close();
    }

index.html The below code is used to open and access the DB

 function onDeviceReady()
 {
  db = window.openDatabase("Bee_dict", "1.0", "Bee_dict", 20000);
  tx.executeSql('SELECT * FROM data WHERE word_id = ?', [1], querySuccess_definition, errorCB);
 }

Cordova version - 2.9

Thanks.


回答1:


In first place, can trying with the next DB file name:

0000000000000001.db

And for load file:

File dbFile = getDatabasePath(".0000000000000001db");

The DB file needs to be in the next route:

yourProyect/assets/0000000000000001.db

I recommend use "SQLitePlugin":

SQLitePlugin GitHub

In the "onDeviceReady()" function i use:

if(!dbCreated){
    db = window.sqlitePlugin.openDatabase("0000000000000001", "1.0", "My Database", -1);
}



回答2:


I faced the same problem, but the answer above does not help me, so I am writing my experience, it may help.

I typically used a Cordova plugin named: me.rahul.plugins.sqlDB at https://github.com/an-rahulpandey/cordova-plugin-dbcopy

It is a plugin focused to copy files from Cordova www folder to the right folder on Android/iphone.

You have first to install the plugin, using:

$ cordova plugin add https://github.com/an-rahulpandey/cordova-plugin-dbcopy.git

Then on Device Ready event:

function onDeviceReady() {
    console.log(">device is ready");
    window.plugins.sqlDB.copy("mydb.sqlite", copySuccess, copyError);
}



回答3:


To access pre-populated database you should first copy the database file in www directory.

Install this plugin DB-Copy plugin and copy the database from www directory into the device, using the copy plugin and then use Sqlite-storage plugin to access the database.

PS. The copy of database from www directory is need it because each OS has different location to store databases...



来源:https://stackoverflow.com/questions/18014419/cant-access-pre-populated-sqlite-database-using-phonegap-cordova-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!