This is for addon “CupidonMC”. I need to convert initDB(), which is in profil.js, because I now need to use Sqlite.jsm. Then, I also need to convert some functions that are doing SQL operations. Here is what I want to do:
- Open SQLite connection and initialize any necessary table. I want to do this in a reusable “promise” function.
- Sqlite.openConnection()
- Test is some table exists
- If table does not exist
- Create table
- else
- Remove any old data in the table
- Do any SQL request
- Close connection
Here is what i tried to do:
initComments: function() {
Components.utils.import("resource://gre/modules/Sqlite.jsm");
return new Promise(function(resolve, reject) {
let conn = yield Sqlite.openConnection({path: "cupidonmc.sqlite"});
let tableExists = yield conn.tableExists("comments");
if (!tableExists) {
yield conn.execute("CREATE TABLE comments " +
"(myName TEXT NOT NULL, targetId TEXT NOT NULL, " +
"myComments TEXT, myType INTEGER, timestamp INTEGER NOT NULL, " +
"PRIMARY KEY (myName, targetId))");
}
else {
yield conn.execute("DELETE FROM comments WHERE timestamp <= :old_time",
{ old_time: (new Date()).getTime() - 91 * 24 * 3600 * 1000 });
}
resolve(conn);
});
}
Actually, this function does not even create the SQLite file when it does not exist. I do not know what is wrong. Here is how I call this promise function:
Components.utils.import("resource://gre/modules/Task.jsm");
Task.spawn(function* () {
try {
let conn = yield CupidonMC_profil.initComments();
// Some operations with conn
}
finally {
yield conn.close();
}
});
Please, let me know what I am doing wrong. In Sqlite.jsm documentation, there is no example similar to this use case.
Thanks.