我的数据库通过 Phonegap 存储在 Android 中的 /data/data/package-name/app_database/file__0/*.db 中
我正在尝试将一些数据存储在我使用phonegap创建的数据库中,例如
document.addEventListener("deviceready", onDeviceReady, false);
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// Query the database
//
function queryDB(tx) {
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
// Query the success callback
//
function querySuccess(tx, results) {
var len = results.rows.length;
console.log("DEMO table: " + len + " rows found.");
for (var i=0; i<len; i++){
console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data);
}
}
// Transaction error callback
//
function errorCB(err) {
console.log("Error processing SQL: "+err.code);
}
// Transaction success callback
//
function successCB() {
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(queryDB, errorCB);
}
// PhoneGap is ready
//
function onDeviceReady() {
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
但是数据库没有被创建。相反,它将它保存在 /data/data/package-name/app_database/file__0/00000000000000001.db
我想将数据保存在 /data/data/package-name/database/Database.db 中
如何操作?
I am trying to store some data in a database which i create using phonegap like
document.addEventListener("deviceready", onDeviceReady, false);
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// Query the database
//
function queryDB(tx) {
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
// Query the success callback
//
function querySuccess(tx, results) {
var len = results.rows.length;
console.log("DEMO table: " + len + " rows found.");
for (var i=0; i<len; i++){
console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data);
}
}
// Transaction error callback
//
function errorCB(err) {
console.log("Error processing SQL: "+err.code);
}
// Transaction success callback
//
function successCB() {
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(queryDB, errorCB);
}
// PhoneGap is ready
//
function onDeviceReady() {
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
But the database is not getting created..Instead it is saving it in /data/data/package-name/app_database/file__0/00000000000000001.db
I want to save the data in /data/data/package-name/database/Database.db
How to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非修改 PhoneGap 源代码,否则无法更改创建数据库的目录。我想更好的问题是为什么要将数据保存在 /data/data/package-name/database/Database.db 而不是 /data/data/package-name/app_database/file__0/00000000000000001.db 中?
更新:现在我更好地理解了要求,您能够做到这一点的唯一方法是为 Android 编写一个插件,将 JavaScript 端连接到本机 Java 边。然后,您可以打开并读取您的服务已创建和填充的 Java 数据库。
Marc Murphy (CommonsWare) 在此线程中详细介绍了原因。
You can't change the directory in which the database is created unless you modify the PhoneGap source code. I guess a better question is why do you want to save the data in /data/data/package-name/database/Database.db instead of /data/data/package-name/app_database/file__0/00000000000000001.db?
Update: Now that I understand the requirement better the only way you are going to be able to do this is to write a plugin for Android that interfaces the JavaScript side to the native Java side. Then you can open and read the DB in Java that your Service has created and populated.
Marc Murphy (CommonsWare) goes over the why in this thread.
请参阅:http://developer.android.com/guide/topics /data/data-storage.html#db 和 http://developer.android.com/guide/developing/tools/adb.html#sqlite。创建数据库时,可以指定数据库名称,会在/data/data/包名/databases/下创建一个以.db为后缀的文件。
抱歉,您提供的代码没有显示数据库是如何创建的。
See: http://developer.android.com/guide/topics/data/data-storage.html#db and http://developer.android.com/guide/developing/tools/adb.html#sqlite. When creating the database, you can specify the database name and a file name with .db will be created in /data/data/package name/databases/.
Sorry, but the code you have provided doesn't show how the database has been created.
我建议您使用 LocalStorage 而不是 sqlite 来实现此目的。
您可以参考本地存储:http://docs.phonegap.com /en/1.0.0/phonegap_storage_storage.md.html
示例:
它提供了一种更好的方式来轻松存储和检索数据 有效地
I would suggest that you use LocalStorage instread of sqlite for this purpose.
You can refer to Local storage at : http://docs.phonegap.com/en/1.0.0/phonegap_storage_storage.md.html
Example :
It provides a better way to store and retrieve your data easily and effectively
正如文档所述,Phonegap并没有真正使用SQLite数据库,而是 WebSQL 实现或 localStorage。
如果你想使用原生 SQLite,可以看看这个插件,数据库操作的语法是与 Phonegap 的存储 API 几乎相同。
As the documentation states, Phonegap doesn't really use a SQLite database, but a WebSQL implementation or localStorage.
If you want to use native SQLite, have a look at this plugin, the syntax for database operations are nearly the same as for the Storage API of Phonegap.