1

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

Joe
  • 11
  • 2
  • Welcome to StackOverflow! Please insert your error so we can understand your problem more clearly and try to create an MCVE ([mcve]) so there is no need for reading unnecessary lines of code. – Hille Oct 08 '18 at 06:20
  • Sorry, I am a beginner on this site. So the error message is: "Error: Table does not exist: cities", but i created erlier. I attached a fully working example. You can copy to an empty html file. – Joe Oct 08 '18 at 06:36
  • You can [edit] your question with the edit button below it to add informations / code or change some text – Hille Oct 08 '18 at 06:39
  • 1
    Thank you for your advice. I did the changes. – Joe Oct 08 '18 at 06:52

0 Answers0