mac eclipse android phonegap与sqlite数据库自动增量和maxid retreivel

时间:2012-09-13 13:04:02

标签: javascript android sqlite cordova

我实际上是dotnet开发人员我现在开始使用eclipse android和phonegap开发移动应用程序我已经在docs.phonegap.com中研究了这些我在实例中发现没有困难,例如相机设备和加速度计。我已成功连接到数据库现在我的问题是如何在创建表中应用自动增量和从数据库中检索自动增量(最大ID +1)给定代码不起作用
我的代码
Javascript代码#

     function onDeviceReady() {
            var db = window.openDatabase('dbNexTrainer','1.0', 'NexTrainer',2 * 1024 * 1024);
            db.transaction(populateDB, errorCB, successCB);
        }
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS tbl_Excercise  (ExId INTEGER PRIMARY KEY AUTOINCREMENT ,ExName ,ExDescription ,ExPreCondition ,ExImage ,IsActive INTEGER,AddDate DATE)');
tx.executeSql('Delete from tbl_Excercise');
tx.executeSql('INSERT INTO tbl_Excercise  VALUES (1,"Excercise 1","Description 1","Pre Condition 1","image/image1.png",1,"12/09/2012 01:00:00")');
tx.executeSql('INSERT INTO tbl_Excercise  VALUES (2,"Excercise 2","Description 2","Pre Condition 2","image/image2.jpg",1,"12/09/2012 01:00:00")');
tx.executeSql('INSERT INTO tbl_Excercise  VALUES (3,"Excercise 3","Description 3","Pre Condition 3","image/image3.jpg",1,"12/09/2012 01:00:00")');
tx.executeSql('INSERT INTO tbl_Excercise  VALUES (4,"Excercise 4","Description 4","Pre Condition 4","image/image4.jpg",1,"12/09/2012 01:00:00")');


第二个问题如何检索最大ID
Javascript代码

   function NewExcercise(){
     var db = window.openDatabase('dbNexTrainer','1.0', 'NexTrainer',2 * 1024 * 1024);
          db.transaction(GetNewExId, errorCB);    
    }
var  GetNewExId=function(tx) 
       {
           var db = window.openDatabase('dbNexTrainer','1.0', 'NexTrainer',2 * 1024 * 1024);
          var sql = "SELECT MAX(ExId)+1 as ExId FROM tbl_Excercise";
           tx.executeSql(sql, [], ReturnNewId, errorCB); 
           return  window.localStorage.getItem('NewExId') 
      }

      var  ReturnNewId  = function(transaction, results) {
   try{       
         var row = results.rows.item(i);          
         window.localStorage.setItem('NewExId', row['ExId']);            
    }
    catch(e){
    alert(e);
    }
    }

1 个答案:

答案 0 :(得分:1)

您在CREATE TABLE语法中有错误,您需要为所有表字段定义数据类型

试试这句话,

tx.executeSql('CREATE TABLE IF NOT EXISTS tbl_Excercise  (ExId INTEGER PRIMARY KEY AUTOINCREMENT ,ExName TEXT ,ExDescription TEXT ,ExPreCondition TEXT ,ExImage BLOB ,IsActive INTEGER,AddDate DATE)');