WebSQL | SQLite -插入"?字符输入数据库

WebSQL | SQLite - Inserting the "?" character into the database

本文关键字:字符输入 数据库 quot SQLite 插入 WebSQL      更新时间:2023-09-26

我正试图将问题插入数据库,但是由于准备好的语句性质而存在一些问题。错误如下:

could not prepare statement (1 near ")": syntax error)

下面是使用的代码:

tx.executeSql("INSERT INTO premade_questions ('Do you like animals?')", 
            [], 
            function() { console.log("Inserted"); },
            function(n, error) { console.log(error.message);    }
        );

当我更改代码以使用准备好的语句时,错误变为:

could not prepare statement (1 near "?": syntax error)

修改后的代码如下:

 tx.executeSql("INSERT INTO premade_questions (?)", 
            ['Do you like animals?'], 
            function() { console.log("Inserted"); },
            function(n, error) { console.log(error.message);    }
        );

我试过逃离?,但没有运气。

您错过了VALUES关键字:

如果您的表只包含问题文本字段,您应该将查询更改为INSERT INTO premade_questions VALUES ('Do you like animals?')

如果您有更多的字段,那么您应该将它们添加到值中,或者指定您只提供问题字段。例如,如果字段名是question,那么您的查询应该是:INSERT INTO premade_questions (question) VALUES ('Do you like animals?') .