[英]How to use pre-populated Sqlite database in cordova android project
I'm creating an android project using Cordova
in which I use a Sqlite
database. 我正在使用Cordova
创建一个android项目,其中使用了Sqlite
数据库。 My database would be read-only as use only can read data from the tables. 我的数据库将是只读的,因为只能使用它才能从表中读取数据。 So my Sqlite
database has to contain some data when it's been installing on user's phone. 所以我的Sqlite
数据库在用户手机上安装时必须包含一些数据。
How Can I use a pre-populated database in my .apk
file? 如何在.apk
文件中使用预填充的数据库?
Add Plugin 添加插件
cordova plugin add https://github.com/millerjames01/Cordova-SQLitePlugin.git cordova插件添加https://github.com/millerjames01/Cordova-SQLitePlugin.git
html HTML
<input id="show" type="button" value="Show">
js JS
function globalError(tx, error)
{
alert("Error: " + error.message);
}
var db = window.openDatabase('TabOrder', '', 'Bar Tab Orders', 2500000);
db.transaction(function(tx) {
tx.executeSql('DROP TABLE IF EXISTS SubmiteData;', null, null, globalError);
tx.executeSql('CREATE TABLE IF NOT EXISTS SubmiteData (SubmiteDataId integer
primary key, UserId text, AuthNo number, LocId number,ProdId number,
CardId number, OrgLat text, OrgLng text, OrgTime text)',
null,
function()
{
SubmiteData("USER1",12345678,23434, 21212, 220232,
"9", "45", "23/06/2014");
},
globalError);
});
function SubmiteData(UserId, AuthNo, LocId,ProdId, CardId, OrgLat, OrgLng, OrgTime){
db.transaction(function(tx){
tx.executeSql('INSERT INTO SubmiteData(UserId, AuthNo, LocId, ProdId, CardId,
OrgLat, OrgLng, OrgTime) VALUES (?,?,?,?,?,?,?,?)', [UserId, AuthNo, LocId,
ProdId, CardId, OrgLat, OrgLng, OrgTime],
null,
globalError
);
});
}
function read(UserId, AuthNo, LocId,ProdId, CardId, OrgLat, OrgLng, OrgTime){
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM SubmiteData',
[],
function(tx, results)
{
for (var i=0; i<results.rows.length; i++)
{
var row=results.rows.item(i);
// alert("Id: " + row['UserId']);
var stringout = "LocId: " + row['LocId'] + "\n";
alert(stringout);
}
},
globalError
);
});
};
$(function()
{
$('#show').click(read);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.