使用的PhoneGap /科尔多瓦无法访问pre填充SQLite数据库Android中 [英] Can't access pre populated SQLite database using PhoneGap/Cordova in Android

查看:148
本文介绍了使用的PhoneGap /科尔多瓦无法访问pre填充SQLite数据库Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正努力创造使用的PhoneGap /科尔多瓦的移动应用程序。我们需要附带一个pre-人口的SQLite数据库这个程序。我们可以使用下面code复制数据库。但是,当我们尝试从复制的数据库访问我们的应用程序表中,我们得到没有这样的表的错误。任何人都可以帮助我们确定的原因是什么?

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 ?

我们使用下面的code数据/数据​​/程序包/数据库文件夹复制到数据库中。

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的 下面code用来打开和访问数据库

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);
 }

科尔多瓦版本 - 2.9

Cordova version - 2.9

感谢。

推荐答案

在第一个地方,可以用下一个DB文件名试图:

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

0000000000000001.db

和负载文件:

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

该数据库文件需要在接下来的路线:

The DB file needs to be in the next route:

yourProyect/assets/0000000000000001.db

我建议用SQLitePlugin:

I recommend use "SQLitePlugin":

<一个href="https://github.com/brodybits/PhoneGap-sqlite-plugin-rebuild/blob/master/Android/assets/www/SQLitePlugin.js"相对=nofollow> SQLitePlugin GitHub上

在onDeviceReady()函数我使用:

In the "onDeviceReady()" function i use:

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

这篇关于使用的PhoneGap /科尔多瓦无法访问pre填充SQLite数据库Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆