首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在sql.js中出现“NOT NULL constraint failed”错误?

为什么在sql.js中出现“NOT NULL constraint failed”错误?
EN

Stack Overflow用户
提问于 2020-11-06 01:36:46
回答 1查看 63关注 0票数 0

我正在尝试使用sql.js https://github.com/sql-js/sql.js在html网页中创建一个具有以下列的表。

代码语言:javascript
复制
<script src="sql.js"></script>
<script>
     var data;
     config = {
          locateFile: filename => `sql.js`
     }
</script>
<script>
initSqlJs(config).then(function (SQL) {
    var db = new SQL.Database();
    db.run(`CREATE TABLE notes (
        id              integer primary key,   /* 0 */
        guid            text not null,         /* 1 */
        mid             integer not null,      /* 2 */
        mod             integer not null,      /* 3 */
        usn             integer not null,      /* 4 */
        tags            text not null,         /* 5 */
        flds            text not null,         /* 6 */
        sfld            integer not null,      /* 7 */
        csum            integer not null,      /* 8 */
        flags           integer not null,      /* 9 */
        data            text not null          /* 10 */
    );`)

    db.run( `INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')`, 123,"abcdef", 12345, 56789, -1, "tags", "hi", 0);

});
</script>

但是得到下面的错误

代码语言:javascript
复制
sql.js:89 Uncaught Error: NOT NULL constraint failed: notes.guid
    at c.handleError (sql.js:89)
    at a.step (sql.js:80)
    at c.run (sql.js:86)
    at <anonymous>:1:5

可以做些什么来消除错误?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-06 01:41:24

可能是因为您提供给run的值应该在单个数组中,而不是作为多个参数传递。

代码语言:javascript
复制
<script src="sql.js"></script>
<script>
     var data;
     config = {
          locateFile: filename => `sql.js`
     }
</script>
<script>
initSqlJs(config).then(function (SQL) {
    var db = new SQL.Database();
    db.run(`CREATE TABLE notes (
        id              integer primary key,   /* 0 */
        guid            text not null,         /* 1 */
        mid             integer not null,      /* 2 */
        mod             integer not null,      /* 3 */
        usn             integer not null,      /* 4 */
        tags            text not null,         /* 5 */
        flds            text not null,         /* 6 */
        sfld            integer not null,      /* 7 */
        csum            integer not null,      /* 8 */
        flags           integer not null,      /* 9 */
        data            text not null          /* 10 */
    );`)

    db.run(
        `INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')`, 
        [123,"abcdef", 12345, 56789, -1, "tags", "hi", 0]
    );
});
</script>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64702428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档