![](/img/trans.png)
[英]Phonegap 3.4 on Android 4.4 with pre-populated SQLite database
[英]Can't access pre populated SQLite database using PhoneGap/Cordova in Android
我们正在尝试使用PhoneGap / Cordova创建一个移动应用程序。 我们需要将此应用与预填充的SQLite数据库一起提供。 我们可以使用以下代码复制数据库。 但是,当我们尝试从应用程序中复制的数据库访问表时,会出现“ No Such Table”错误。 谁能帮助我们确定原因?
我们使用以下代码将数据库复制到data / data / package_name / databases文件夹。
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以下代码用于打开和访问数据库
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
谢谢。
首先,可以尝试使用下一个数据库文件名:
0000000000000001.db
对于加载文件:
File dbFile = getDatabasePath(".0000000000000001db");
该数据库文件需要在下一条路径中:
yourProyect/assets/0000000000000001.db
我建议使用“ SQLitePlugin”:
在“ onDeviceReady()”函数中,我使用:
if(!dbCreated){
db = window.sqlitePlugin.openDatabase("0000000000000001", "1.0", "My Database", -1);
}
我遇到了同样的问题,但是上面的答案对我没有帮助,所以我写我的经验可能会有所帮助。
我通常使用https://github.com/an-rahulpandey/cordova-plugin-dbcopy上的名为me.rahul.plugins.sqlDB的Cordova插件
它是一个插件,旨在将文件从Cordova www文件夹复制到Android / iphone上的正确文件夹。
您首先需要使用以下步骤安装插件:
$ cordova plugin add https://github.com/an-rahulpandey/cordova-plugin-dbcopy.git
然后在“设备就绪”事件上:
function onDeviceReady() {
console.log(">device is ready");
window.plugins.sqlDB.copy("mydb.sqlite", copySuccess, copyError);
}
要访问预先填充的数据库,您应该首先将数据库文件复制到www目录中。
安装此插件DB-Copy插件 ,然后使用copy插件将www目录中的数据库复制到设备中,然后使用Sqlite-storage插件访问数据库。
PS。 需要从www目录复制数据库,因为每个操作系统都有不同的位置来存储数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.