【问题标题】:mac eclipse android phonegap with sqlite database autoincrement and maxid retreivelmac eclipse android phonegap 与 sqlite 数据库自动增量和 maxid retreivel
【发布时间】:2012-09-13 21:09:01
【问题描述】:

我实际上是 dotnet 开发人员,现在我开始使用 eclipse android 和 phonegap 开发移动应用程序,我在 docs.phonegap.com 中研究过这些我发现示例中没有困难,即相机设备和加速度计。我已经成功连接到数据库现在我的问题是如何在创建表中应用自动增量并从数据库中检索(max 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);
    }
    }

【问题讨论】:

    标签: javascript android sqlite cordova


    【解决方案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)');
    

    【讨论】:

    • 创建表工作正常,我如何在查询中自动递增并检索 maxid
    • SELECT MAX(ExId) as ExId FROM tbl_Excercise
    • 是的,我确定它可以正常工作而无需定义列我只定义 isavtice 整数和 adddate 作为日期
    • @skhurams,您可以忽略该字段并像这样将插入查询写入 tbl_Excercise(字段名称...)值(值);
    • 你写的(代码),你能告诉我声明吗?
    猜你喜欢
    • 1970-01-01
    • 2013-10-13
    • 2013-12-03
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 2012-03-28
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多