AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.04k stars 659 forks source link

last insert id with localStorgae #382

Open dnaura opened 9 years ago

dnaura commented 9 years ago

Hello , I want to have the last insert id of a table of a localstorage database. Here is the code:

alasql('CREATE localStorage DATABASE IF NOT EXISTS Eisenhower');
alasql('ATTACH localStorage DATABASE Eisenhower AS myEisenhower');
alasql('SET AUTOCOMMIT ON');
alasql('USE myEisenhower');
var db = alasql.Database('myEisenhower');
var sql = "CREATE TABLE IF NOT EXISTS taches ( id_tache SMALL AUTO_INCREMENT PRIMARY KEY, description STRING, urgence REAL, importance REAL, ampleur REAL )";
db.exec(sql,'', function(data){ console.log("database created"); });
alert(db.databases.tables.taches.identities.id_tache.value);

The last command sends an error as it does not recognize property 'tables' of undefined.

I must do something wrong; or is it a bug? Thanks. Best regards. David

mathiasrw commented 9 years ago

Thanks for describing the question very well.

To access the database tables you must replace db with alasql in the last line:

alert(alasql.databases.tables.taches.identities.id_tache.value);

Please reopen the issue if it does not help...

dnaura commented 9 years ago

Thanks, I had also tried this, but it does not work. I am wondering if I am using the wrong syntax to mix localStorage attachement (the 4 first lines) and javascript assignement ( var db = alasql.Database('myEisenhower'); ). If it is wrong I can't figure out how to access classes of database attached to localStorage. I did not find anything in documentation,wiki and google. Thanks. David