2

I am trying to use SQLite with React native to insert some values into a table

In my main page I am creating the table like so...

db.transaction(tx => {
  tx.executeSql(
    `create table if not exists puzzles (
        id primary key not null, 
        level int not null, 
        image varchar(512) NOT NULL,
        imageSolution varchar(512) NULL,
        puzzleAnswer varchar(16) NULL,
        type varchar(16) NULL,
        availableLetters varchar(16) NULL,
        charactersGiven varchar(4) NULL);`
  );
});

In my page component I am trying to insert using the following:

    db.transaction(tx => {
      for (var puz of puzzlesFiltered) {
        tx.executeSql(
          `insert into
        puzzles (id, image, imageSolution, type, puzzleAnswer, level, charactersGiven, availableLetters)
        VALUES (?,?,?,?,?,?,?,?) WHERE NOT EXISTS(SELECT 1 FROM puzzles WHERE id = ${
          puz.id
        })`,
          [
            puz.id,
            puz.image,
            puz.imageSolution,
            puz.type,
            puz.puzzleAnswer,
            puz.level,
            puz.charactersGiven,
            puz.charactersGiven
          ],
          (tx, results) => {
            console.log("Results", results.rowsAffected);
          },
          (err) => {
            console.error(err);
          }
        );
      }
    });

I am getting the following error message returned, which I have no idea what it means, can anyone help?

Also, is there a better way to insert an array, rather than looping through each row?

[16:07:09] WebSQLTransaction { "_complete": false, "_error": null, "_running": true, "_runningTimeout": false, "_sqlQueue": Queue { "first": undefined, "last": undefined, "length": 0, }, "_websqlDatabase": WebSQLDatabase { "_currentTask": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, "_db": SQLiteDatabase { "_closed": false, "_name": "db.db", }, "_running": true, "_txnQueue": Queue { "first": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, }, "last": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, }, "length": 1, }, "version": "1.0", }, }

1 Answer 1

2

I'm not sure your query is supported by SQLite.

You could try something like:

INSERT INTO puzzles
(id, image, imageSolution, type, puzzleAnswer, level, charactersGiven, availableLetters)
SELECT ?,?,?,?,?,?,?,?
WHERE NOT EXISTS(SELECT 1 FROM puzzles WHERE id = ${puz.id})
Sign up to request clarification or add additional context in comments.

3 Comments

i have some issue with sqlite, i create the insert fetch delete functions and for some reason it doesn't work for me. can you help me please ?
that there is no insert into my db file . do have facebook or something that i can connect with u to show u my problem.
Please rather open a dedicated SO question and share your code.

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.