问题描述
我希望使用 WebSQL 在 Android PhoneGap 应用程序中存储数据,但不知道从哪里开始.由于应用程序处于离线状态,因此无需从 Web 检索数据,并且我想要一个可以查询的预填充数据库.几个问题:如何创建预先填充的数据库?(应该是什么文件格式)如果有两个因素可以改变,我如何使用 SQL 从数据库中获取这些因素的组合结果.如果用户从 Android 设备的设置"菜单中清除应用数据,数据库是否会被擦除?感谢任何帮助.谢谢
I'm looking to use a WebSQL to store data in an Android PhoneGap app but dunno where to start. There is no need to retrieve data from the web as the application is offline, and I want to have a prepopulated database that can be queried. A few questions:How do you create a pre-populated database? (what kind of file format should it be)If there are two factors can be changed, how do I get the result of a combination of these factors from the database using SQL.Will the database be wiped if the user clears app data from the Settings menu on an Android device?Grateful for any help. thanks
推荐答案
在 PhoneGap 应用程序中预填充 SQLite 数据库
1) 您可以在 SQLite Manager 或者如果您的应用中已有数据库,您可以直接获取数据库文件.
Prepopulate SQLite DataBase in PhoneGap Application
1) You can create a basic database for app with the help of tools like SQLite Manager or if you already have a database in your app you can directly get the database files.
2) 然后你需要通过 CLI 安装 Cordova/PhoneGap SQLitePlugin.(Cordova/PhoneGap SQLitePlugin)
2) Then you need to install Cordova/PhoneGap SQLitePlugin by CLI . (Cordova/PhoneGap SQLitePlugin)
cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin
3) 在您的 html 中添加以下脚本文件.
3) Addd following script file in your html.
<script src="plugins/com.brodysoft.sqlitePlugin/www/SQLitePlugin.js"></script>
4) 如果您正在为模拟器工作,则使用命令提示符复制您的数据库,我已在资产文件夹中添加了 myDB.sqlite 然后运行.
4) If you are working for emulator then copy your databas using command prompt, I have added myDB.sqlite in asset folder then run .
adb push myDB.sqlite /data/data/com.my_app.my_app/databases/myDB.sqlite
5) 对于设备,只需将它们放入应用程序包中,即 Android 的资产文件夹.你需要 myDB.sqlite 和 file__0/0000000000000001.sqlite 文件.
5) For device, just put them in to bundle of app that is assets folder in case of Android . you need myDB.sqlite and file__0/0000000000000001.sqlite files.
6) 现在您必须在应用程序首次启动时复制应用程序本机位置的文件,并确保在您的第一次 SQLite 查询之前复制这些文件.要复制这些文件,您可以根据您的环境使用以下代码片段.爪哇(安卓).更多...
6) Now you have to copy the files at the native location of application at the time of first boot of application, and make sure that the files should copy before your very first SQLite query. To copy these files you can use the following code snippet based on your environment. JAVA (Android). More...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
try {
String pName = this.getClass().getPackage().getName();
this.copy("myDB.sqlite", "/data/data/" + pName + "/databases/");
this.copy("0000000000000001.sqlite", "/data/data/" + pName + "/app_database/file__0/");
} catch (IOException e) {
e.printStackTrace();
}
super.loadUrl(Config.getStartUrl());
}
void copy(String file, String folder) throws IOException {
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists()) {
CheckDirectory.mkdir();
}
InputStream in = getApplicationContext().getAssets().open(file);
OutputStream out = new FileOutputStream(folder + file);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in .close();
out.close();
}
7) 然后在您的 js 文件中添加代码片段.
7) Then add snippet in your js file.
$(document).ready(function(e){
document.addEventListener("deviceready", dbConnection, false);
})
function dbConnection(){
db = window.sqlitePlugin.openDatabase("myDB.sqlite", "1.0", "DB", 2000000);
db.transaction(function(tx) {
tx.executeSql("SELECT * from table", [], function(tx, res) {
for(var i=0; i<res.rows.length; i++){ ;
console.log("RESULT:" + res.rows.item(i)['field_name']);
}
});
});
}
这篇关于用于 PhoneGap 应用程序的 WebSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!