I want to use sql web but I am not able to create table or insert information in it

1
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,null);
})

This is my connection code, it creates the database but it is not creating the table, what can it be? The way I am doing is wrong? When I open the developer tools and go to applications I find the database that I have created but there are no tables

    
asked by anonymous 19.05.2017 / 15:56

1 answer

3

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

    
19.05.2017 / 17:03