为什么在 sql.js 中出现“NOT NULL 约束失败”错误?

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

<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>

但出现以下错误


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

可以采取什么措施来消除错误?


谢谢


白衣非少年
浏览 94回答 1
1回答

青春有我

可能是因为您提供的值run应该位于单个数组中,而不是作为多个参数传递。<script src="sql.js"></script><script>&nbsp; &nbsp; &nbsp;var data;&nbsp; &nbsp; &nbsp;config = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locateFile: filename => `sql.js`&nbsp; &nbsp; &nbsp;}</script><script>initSqlJs(config).then(function (SQL) {&nbsp; &nbsp; var db = new SQL.Database();&nbsp; &nbsp; db.run(`CREATE TABLE notes (&nbsp; &nbsp; &nbsp; &nbsp; id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer primary key,&nbsp; &nbsp;/* 0 */&nbsp; &nbsp; &nbsp; &nbsp; guid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text not null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* 1 */&nbsp; &nbsp; &nbsp; &nbsp; mid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;integer not null,&nbsp; &nbsp; &nbsp; /* 2 */&nbsp; &nbsp; &nbsp; &nbsp; mod&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;integer not null,&nbsp; &nbsp; &nbsp; /* 3 */&nbsp; &nbsp; &nbsp; &nbsp; usn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;integer not null,&nbsp; &nbsp; &nbsp; /* 4 */&nbsp; &nbsp; &nbsp; &nbsp; tags&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text not null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* 5 */&nbsp; &nbsp; &nbsp; &nbsp; flds&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text not null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* 6 */&nbsp; &nbsp; &nbsp; &nbsp; sfld&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer not null,&nbsp; &nbsp; &nbsp; /* 7 */&nbsp; &nbsp; &nbsp; &nbsp; csum&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer not null,&nbsp; &nbsp; &nbsp; /* 8 */&nbsp; &nbsp; &nbsp; &nbsp; flags&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;integer not null,&nbsp; &nbsp; &nbsp; /* 9 */&nbsp; &nbsp; &nbsp; &nbsp; data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text not null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* 10 */&nbsp; &nbsp; );`)&nbsp; &nbsp; db.run(&nbsp; &nbsp; &nbsp; &nbsp; `INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)&nbsp; &nbsp; VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')`,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [123,"abcdef", 12345, 56789, -1, "tags", "hi", 0]&nbsp; &nbsp; );});</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript