2014-01-22 66 views
0

我有一個webapp,我試圖設置一個SQLite數據庫。在這一點上,我建立一個基金會非常簡單。此時只有兩個表格。一個表使用外鍵約束來指向另一個表。我遇到的問題是當我嘗試插入數據時,我總是收到錯誤Error processing SQL: could not execute statement due to a constraint failure (19 constraint failed) -- Code: 6。顯然,代碼6表示表被鎖定。如果我可以成功地將值插入它,它怎麼能被鎖定?困惑......創建SQLite數據庫並嘗試添加記錄 - 拋出erorrs

我的代碼...

我成立了這個表:

// Create a system table, if it doesn't exist 
    db.transaction(function(tx){ 
    tx.executeSql('CREATE TABLE IF NOT EXISTS system(systemID TEXT PRIMARY KEY, numZones INT NULL, numHeads INT NULL)', [],nullHandler,errorHandler); 
    },errorHandler, successCallBack); 


    // Create a work order table, if it doesn't exist 
    db.transaction(function(tx){ 
    tx.executeSql('CREATE TABLE IF NOT EXISTS wo(woID_id TEXT PRIMARY KEY, woType TEXT NOT NULL, systemID_fk TEXT NOT NULL, FOREIGN KEY (systemID_fk) REFERENCES system(systemID))', [],nullHandler,errorHandler); 
    },errorHandler, successCallBack); 

想必現在我有兩個表,一個具有指向其他表中的字段。我正在提供一個JSon提要,解析它,並試圖將它放到這兩個表中。下面是語法分析的代碼:

function GetSystems(){ 

    // First we see if there are credentials stored. If not, we don't try to retrieve the work orders. 

    db.transaction(function(transaction){ 
    transaction.executeSql('SELECT * FROM Creds;',[], function(transaction, result) { 
     // If the user hasn't entered their creds yet, we create a new record, otherwise update the current one. 
     if(result.rows.length != 0){ 
     var row; 
     db.transaction(function(transaction){ 
      transaction.executeSql('SELECT * FROM Creds where id=1;',[],function(transaction, result) { 
      $.getJSON(baseURL + "get-wos/?callback=?", { username:result.rows.item(0).username, password:result.rows.item(0).password }, function(data) { 
       $.each(data, function(i, obj) { 
       db.transaction(function(transaction){ 
        transaction.executeSql('INSERT INTO system(systemID, numZones, numHeads) VALUES (?, null, null)', [obj.systemID], nullHandler, errorHandler); 
        transaction.executeSql('INSERT INTO wo (woID, woType, systemID_fk) ' + 
             'VALUES ((SELECT systemID FROM system WHERE systemID = ' + obj.systemID + '), ?, ?)', 
             [obj.woID, obj.woType], nullHandler, errorHandler); 
       }) 
       }); 
      }); 
      }); 
     }); 
     } 
    }); 
    }); 
} 

當我運行上面的代碼中,systems正確加載,但wos都沒有。我對這個問題的研究告訴我,我可能會遇到一些問題。一個建議是表中可能已經有數據。我通過使用drop tables函數完全清除數據庫(我使用Chrome開發工具來調查數據庫)來修復此問題。

所以真的,我不知道我做錯了什麼。我的語法對於插入外鍵約束是不正確的嗎?

解決

我偶然發現this線程和@havexz提到在插入變量沒有周圍的引號。我看着我的,它也有同樣的問題。這是我的編輯插入添加外鍵的記錄。注意systemID='",而不是原來的只是systemID="。我錯過了我的變量的單引號。

db.transaction(function(transaction){ 
    transaction.executeSql("INSERT INTO wo (woID, woType, systemID_fk) " + 
          "VALUES (?, ?, (SELECT systemID FROM system WHERE systemID='" + obj.systemID + "'))", [obj.woID, obj.woType], nullHandler, errorHandler); 
}); 

回答

0

INSERT wo的參數順序是否正確?它看起來像是將systemID放入woID字段,而不是systemID_fk,它具有約束條件。它應該是:

transaction.executeSql('INSERT INTO wo (woID, woType, systemID_fk) ' + 
         'VALUES (?, ?, (SELECT systemID FROM system WHERE systemID = ' + obj.systemID + '))', 
         [obj.woID, obj.woType], nullHandler, errorHandler); 
+0

我甚至沒有注意到那部分是倒退的。但是,在改變順序之後,它仍然沒有改變任何東西。現在,實際上,我收到一個錯誤,說'錯誤處理SQL:無法準備語句(1沒有這樣的列:R1157) - 代碼:5',緊接着原始的約束錯誤。所以,這就像它試圖去做,但不能到達那裏...... – Garfonzo

相關問題