【发布时间】:2014-10-27 06:56:32
【问题描述】:
我正在使用Cordova 创建一个android 项目,其中我使用Sqlite 数据库。我的数据库将是只读的,因为只能从表中读取数据。所以我的Sqlite 数据库在安装到用户手机上时必须包含一些数据。
如何在我的.apk 文件中使用预填充的数据库?
【问题讨论】:
标签: android database sqlite cordova
我正在使用Cordova 创建一个android 项目,其中我使用Sqlite 数据库。我的数据库将是只读的,因为只能从表中读取数据。所以我的Sqlite 数据库在安装到用户手机上时必须包含一些数据。
如何在我的.apk 文件中使用预填充的数据库?
【问题讨论】:
标签: android database sqlite cordova
添加插件
cordova 插件添加https://github.com/millerjames01/Cordova-SQLitePlugin.git
html
<input id="show" type="button" value="Show">
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);
});
【讨论】: