Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建SQLite数据库并尝试添加记录-引发错误_Java_Sqlite - Fatal编程技术网

Java 创建SQLite数据库并尝试添加记录-引发错误

Java 创建SQLite数据库并尝试添加记录-引发错误,java,sqlite,Java,Sqlite,我有一个webapp,我正试图用它建立一个SQLite数据库。在这一点上,我很容易建立一个基础。此时只有两个表。一个表使用外键约束指向另一个表。我遇到的问题是,当我尝试插入数据时,我总是收到错误处理SQL:由于约束失败而无法执行语句19约束失败-代码:6。显然,代码6表示该表已锁定。如果我能成功地向其中插入值,如何锁定它?困惑 我的代码 我用这个设置表格: // Create a system table, if it doesn't exist db.transaction(functi

我有一个webapp,我正试图用它建立一个SQLite数据库。在这一点上,我很容易建立一个基础。此时只有两个表。一个表使用外键约束指向另一个表。我遇到的问题是,当我尝试插入数据时,我总是收到错误处理SQL:由于约束失败而无法执行语句19约束失败-代码:6。显然,代码6表示该表已锁定。如果我能成功地向其中插入值,如何锁定它?困惑

我的代码

我用这个设置表格:

 // 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);
                })
              });
            });
          });
        });
      }
    });
  });
}
当我运行上述代码时,系统已正确加载,但WO未正确加载。我对这个问题的研究告诉我,我可能有一些问题。一个建议是表中可能已经有数据。我修复了这个问题,通过使用droptables函数完全清除数据库,我使用chromedev工具来调查数据库

所以说真的,我不确定我做错了什么。插入外键约束的语法是否不正确

解决

我偶然发现了这个线程,@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);
 });

插入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);

我甚至没有注意到那个部分是向后的。然而,在改变了顺序之后,它仍然没有改变任何东西。现在,事实上,我得到一个错误,说错误处理SQL:无法准备语句1没有这样的列:R1157-代码:5,后面是约束的原始错误。所以,这就像它试图做到这一点,但就是不能达到那里。。。