2

我正在使用 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意味着双倍等等....每次。

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

4

2 回答 2

2

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

于 2012-11-18T13:18:30.773 回答
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);});
于 2012-11-18T13:34:28.333 回答