2012-11-18 19 views
2

我正在开发一个使用phonegap的应用程序,我正在使用数据库作为sqlite。 我创建使用下面的命令表:每当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);}); 
} 

..而且使用该功能来从数据库中获得valuse ..

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++) { 
     ...... } 
}} 

我得到一个意想不到的错误是结果集的长度每次增加 意味着如果运行应用程序第一次它显示正确的值,当我运行我t再次显示resultset = results.rows.length + results.rows.length的长度意味着每次都是double等。

如果有人知道发生了什么问题,请帮助我。

回答

2

AddDBValues在应用程序的每次运行中都会被调用吗? IF NOT EXISTS对插入语句没有影响。

+0

是AddDBValues每次被调用。但如果我使用“IF NOT EXISTS”,则不应每次创建表格。我对吗?? –

+0

感谢它的工作.. –

1

数据库在运行之间是否持久?如果是这样,那么数据会翻倍,因为不会丢掉表格。在你AddDBvalues()功能DROP ...命令被注释掉。

//tx.executeSql('DROP TABLE IF EXISTS Vaccin',nullHandler,nullHandler); 

不相关,但你也有一个可能的SQL注入漏洞。变量cn应作为绑定变量传入,而不是简单地作为字符串添加到SQL中。

transaction.executeSql("SELECT * from Vaccin WHERE CCountryid='"+cn+"';",[],  dataHandler, errorHandler);}); 
+0

感谢您的答案。 –