我正在使用phonegap开发一个应用程序,并且使用数据库作为sqlite。 我使用以下命令创建了一个表:
var dbb;
var shortName = 'Vaccine1';
var version = '2.0';
var displayName = 'vaccine';
var maxSize = 100000;
dbb = window.openDatabase(shortName, version, displayName,maxSize);
并使用此函数插入值..
function AddDBvalues()
{
dbb.transaction(function(tx){
//tx.executeSql( 'DROP TABLE IF EXISTS Vaccin',nullHandler,nullHandler);
tx.executeSql( 'CREATE TABLE IF NOT EXISTS Vaccin(Vday INTEGER NOT NULL,VName TEXT NOT NULL, CCountryid INTEGER NOT NULL , Sex TEXT NOT NULL)', [],nullHandler,errorHandler);},errorHandler,successCallBack);
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["0","BCG","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["0","OPV dose 1 of 5","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["1","Hepatites B dose 1 of 2","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["42","DPT dose 1 of 3","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["42","OPV dose 2 of 5","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["70","DPT dose 2 of 3","91","both"], nullHandler,errorHandler);});
}
并使用此函数从数据库获取值..
function ShowValue()
{
var cnn = sessionStorage.getItem('cid');//getting from session
var cn=parseInt(cnn);
alert (cn); //always show correct value
dbb.transaction(
function (transaction) {
transaction.executeSql("SELECT * from Vaccin WHERE CCountryid='"+cn+"';",[], dataHandler, errorHandler);});
function dataHandler(transaction, results){
alert("first method" + results.rows.length);
for (var i=0; i<results.rows.length; i++) {
...... }
}}
我收到一个意外错误,结果集的长度每次都会增加 意味着如果第一次运行应用程序,它会显示正确的值,当我再次运行它时,它只会显示结果集的长度 = results.rows.length+results.rows.length 意味着双倍等等......每次。
如果有人知道出了什么问题,请帮助我。
最佳答案
每次运行应用程序时都会调用 AddDBValues 吗? IF NOT EXISTS 对插入语句没有影响。
关于javascript - PhoneGap 上的 SQLite 数据库结果集长度每次都会增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13439929/