I try to use alaSQL, but not working well for me.
My scenario is create a localstorage db when is not exists. In this case i "init" the database. It means i create all tables and insert some records. Finally i select some data and send to the "display".
First run is everything ok, but next time (when i click the browser's refresh button) i get an error message. "Error: Table does not exist: cities", but i created erlier. Why can i get this message?
Please check my example:
<!DOCTYPE html>
<html>
<head>
<title>ALASQL</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/alasql"></script>
<script>
var db;
function log(msg) {
console.log("log: "+msg);
};
function initDB() {
log("initDB");
alasql.options.autocommit = true;
alasql("CREATE localStorage DATABASE IF NOT EXISTS lsdb");
alasql("ATTACH localStorage DATABASE lsdb AS db");
db = alasql.Database("db");
try {
db.exec("CREATE TABLE params (par string, val string)");
log("creDB");
db.exec("insert into params values ('db','1.0')");
db.exec("CREATE TABLE cities (id number, city string, pop number)");
db.exec("INSERT INTO cities VALUES (1,'Paris',2249975),(2,'Berlin',3517424),(3,'Madrid',3041579)");
}
catch (err) {
log(err);
};
};
function getData(sql) {
log("getData");
res = db.exec(sql);
return res;
};
function onLoad() {
initDB();
try {
res = getData("SELECT * FROM cities WHERE pop > 3500000 ORDER BY pop DESC");
document.getElementById("res").textContent = JSON.stringify(res, null, 2);
}
catch (err) {
document.getElementById("res").textContent = err;
};
};
</script>
</head>
<body onload="onLoad()">
<xmp id="res"></xmp>
</body>
</html>
Thanks for any advice.
Joe