When working with WebSql
and an error occurs, add a callback
to the error return when calling transaction.executeSql
:
let db = openDatabase('produto_teste','1.0','banco de dados para cadastro do produto',2*1024*1024);
db.transaction(function(tx){
tx.executeSql('CREATE TABLE IF NOT referencia(\
id INTEGER NOT NULL AUTOINCREMENT,\
nome VARCHAR NOT NULL,\
marca VARCHAR NOT NULL,\
categoria VARCHAR NOT NULL,\
template VARCHAR NOT NULL,\
grade INTEGER NOT NULL\
);',[[]],null, function (t, e) { console.error(e); });
})
While doing this, I would have seen the following
SQLError {code: 5, message: "could not prepare statement (1 near "referencia": syntax error)"}
SQLError {code: 5, message: "could not prepare statement (1 near "AUTOINCREMENT": syntax error)"}
SQLError {code: 5, message: "number of '?'s in statement string does not match argument count"}
So if we solve these problems, we will have the following script:
let db = openDatabase('produto_teste','1.0','banco de dados para cadastro do produto',2*1024*1024);
db.transaction(function(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS referencia(\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
nome VARCHAR NOT NULL,\
marca VARCHAR NOT NULL,\
categoria VARCHAR NOT NULL,\
template VARCHAR NOT NULL,\
grade INTEGER NOT NULL\
);',[], null, null);
})
But try to look at a detail, WebSQL
does not have a good CrossBrowser
support, your code will not work in IE
and Firefox
.
I advise you to use LocalDB.js
instead of WebSQL