0

I am trying to create a table with following columns in html webpage using sql.js https://github.com/sql-js/sql.js .

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

But getting following errors

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

What can be done to remove the errors?

Thanks

1 Answer 1

2

Likely because the values you provide to run should be in a single array, not passed as multiple arguments.

<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>
Sign up to request clarification or add additional context in comments.

Comments

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.