2009-08-08 13 views
1

私はGoogleの歯車(Sqliteを)でテーブルを作成しました:Javascript関数とDBクエリツールでlast_insert_row()を使用した場合の違いは?

db.execute('create table if not exists SPALINKS (link_id int PRIMARY KEY, sid1 int, sid2 int, label text, user text, Timestamp int'); 

(FF)でGoogleのGearsのデータベースクエリツールを使用する場合は、 '最後の挿入-ROWID' で作業罰金を挿入します。 Javascriptのインサートは、「last-insert-rowid」を使用しない限り、うまく動作します。しかし、「最後の挿入-ROWIDを」を使用する場合、それはもう動作しません:

var sql_stmt = 'INSERT INTO SPALINKS (link_id, sid1, sid2, label, user, Timestamp) VALUES (last_insert_rowid(),?,?,?,?,?)'; 
var arg_array = [sid1, sid2, label, user, creation_time]; 
db.execute(sql_stmt, arg_array); 

Error: Database operation failed. ERROR: constraint failed DETAILS: constraint failed 

なぜではなく、作成されたSQL文では、DBツールでのSQLiteの「最後の挿入-ROWIDの仕事の罰金を行いますJavascript関数の内部から実行されますか?

答えて

0

この状況ではlast_insert_rowid()を使用する必要はありません。なぜ作るだけINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT

// if you writing for android and you have something like this: 
//document.addEventListener("deviceready", onDeviceReady, false); 
//function onDeviceReady(){ 
// make sure you comment it if you debugging in browser 

// connection to the databse 
var db = window.openDatabase("demo.db", "1.0", "Demo Database", 200000); 

// There is problem with SQLite recognize new Date() format so I include http://code.google.com/p/datejs/ library in my project and format the date to be recognizable by SQLite 

function nowTime(){ 
     return new Date().toString("yyyy-MM-dd HH:mm:ss"); 
    } 

function errorCallBack(err) { 
      alert("Error processing SQL: " + err.code); 
     } 
function callBack(){ 

    function readTransactionError(err){ 
     alert('Read Transaction Error: ' + err); 
    } 

// you can make your vars global if you want to use them out side of function!!! 

    function querySuccess(tx, results) { 
     var len = results.rows.length; 
     for(var i = 0; i < len; i++) { 
      var sid1Var = results.rows.item(i).sid1; 
      var sid2Var = results.rows.item(i).sid2; 
      var labelVar = results.rows.item(i).label; 
      var userNameVar = results.rows.item(i).userName; 
      var timeStampVar = results.rows.item(i).timeStamp; 
    } 
    } 

tx.readTransaction('select * from SPALINKS;', [], querySuccess, readTransactionError); 
} 
function functionName(sid1, sid2, label, userName) { 
    db.transaction(function(tx) { 
     db.executeSql('create table if not exists SPALINKS (link_id int INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sid1, sid2, label, userName, timeStamp'); 

     db.executeSql('insert into SPALINKS (sid1, sid2, label, userName, timeStamp) VALUES (?, ?, ?, ?, ?)', [sid1, sid2, label, userName, nowTime()], callBack, errorCallBack); 
    }); 
} 
// assuming you use jQuery 
$('#selectorID').click(function(){ 
functionName(sid1, sid2, label, userName); 
}); 

あなたはUPDATEのような状況でlast_insert_rowid()を使用することができます。

function updateFunction() { 
db.transaction(function(tx) { 
    tx.executeSql('update SPALINKS set timeStamp = "' + nowTime() + '" WHERE link_id = last_insert_rowid()'); 
}); 
} 

私はこれが仕事に行くされて願っています。

0

問題は、それぞれの列がAUTOINCREMENTとして定義されていなかったようです。

関連する問題