0

I have a webapp that I'm trying to set up an SQLite database with. At this point, I have it very simple to build a foundation. At this point there are only two tables. One table uses a foreign key constraint to point to the other table. The problem I am having is when I try to insert data, I always receive the error Error processing SQL: could not execute statement due to a constraint failure (19 constraint failed) -- Code: 6. Code 6, apparently, means the table is locked. How can it be locked if I can successfully insert values into it? Confused...

My code...

I set up the tables with this:

 // 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);

Presumably now I will have two tables, one having a field that points to the other table. I am pulling in a JSon feed, parsing it, and trying to put it into these two tables. Here's the code for that parsing:

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);
                })
              });
            });
          });
        });
      }
    });
  });
}

When I run the above code, the systems are loaded properly but the wos are not. My research into this issue tells me that I might be having a few issues. One suggestion is that there may already be data in the table. I fixed that by having a drop tables function to clear out the database entirely (I use Chrome dev tools to investigate the db).

So really, I'm not sure what I'm doing wrong. Is my syntax incorrect for inserting a foreign key constraint?

Solved

I stumbled upon this thread and @havexz mentioned the variable in the insertion didn't have quotes around it. I looked at mine and it had the same problem. Here's my edited insert to add a record with a foreign key. Notice the systemID='" instead of the original which was simply systemID=". I was missing the single quotes around my variable.

 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);
 });

1 Answer 1

0

Is the order of the parameters correct for the INSERT wo? It looks like you're putting the systemID into the woID field rather than systemID_fk which has the constraint. Should it be:

transaction.executeSql('INSERT INTO wo (woID, woType, systemID_fk) ' +
                       'VALUES (?, ?, (SELECT systemID FROM system WHERE systemID = ' + obj.systemID + '))',
                       [obj.woID, obj.woType], nullHandler, errorHandler);
Sign up to request clarification or add additional context in comments.

1 Comment

I hadn't even noticed that part was backwards. However, after changing the order, it still didn't change anything. Now, in fact, I get an error saying Error processing SQL: could not prepare statement (1 no such column: R1157) -- Code: 5 followed by the original error of constraints. So, it's like its trying to do it but just can't get there...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.