2009-08-08 3 views
1

나는 구글 기어 (SQLite는)에서 테이블을 만들었습니다DB 쿼리 도구에서 Javascript 함수에서 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)에서 쿼리 도구를 사용하여, '마지막 삽입-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 

DB를 도구를 사용하여 SQLite는의 '마지막 삽입-ROWID'잘 동작은 아니지만 SQL 문에서 생성 않는 이유 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

문제는 각 열이 자동 감사로 정의되지 않은 것 같습니다.