反应本机SQLite插入语句不起作用

时间:2018-11-25 16:12:46

标签: javascript sqlite react-native

我正在尝试将SQLite与React native一起使用以在表中插入一些值

在主页上,我像这样创建表格...

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

我要在页面组件中使用以下内容进行插入:

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

我收到以下错误消息,我不知道这是什么意思,任何人都可以帮忙吗?

还有,有比插入遍历每一行更好的方法吗?

  

[16:07:09] WebSQLTransaction {“ _complete”:false,“ _ error”:null,   “ _running”:true,“ _runningTimeout”:false,“ _sqlQueue”:队列{       “第一”:未定义,       “ last”:未定义,       “ length”:0,},“ _websqlDatabase”:WebSQLDatabase {       “ _currentTask”:TransactionTask {         “ errorCallback”:[匿名函数],         “ readOnly”:否,         “ successCallback”:[匿名函数],         “ txnCallback”:[匿名函数],       },       “ _db”:SQLiteDatabase {         “ _closed”:否,         “ _name”:“ db.db”,       },       “ _running”:是的,       “ _txnQueue”:队列{         “第一”:对象{           “项目”:TransactionTask {             “ errorCallback”:[匿名函数],             “ readOnly”:否,             “ successCallback”:[匿名函数],             “ txnCallback”:[匿名函数],           },         },         “最后”:对象{           “项目”:TransactionTask {             “ errorCallback”:[匿名函数],             “ readOnly”:否,             “ successCallback”:[匿名函数],             “ txnCallback”:[匿名函数],           },         },         “长度”:1       },       “ version”:“ 1.0”,},}

1 个答案:

答案 0 :(得分:1)

我不确定SQLite是否支持您的查询。

您可以尝试以下方法:

INSERT INTO puzzles
(id, image, imageSolution, type, puzzleAnswer, level, charactersGiven, availableLetters)
SELECT ?,?,?,?,?,?,?,?
WHERE NOT EXISTS(SELECT 1 FROM puzzles WHERE id = ${puz.id})