db.run to CREATE TABLE
Created by: swinston100
Hi
I would like to create a table if it doesn't exist. My code is not working.
function(db, data, done){
db.run("CREATE TABLE IF NOT EXISTS revenue(id serial primary key, \"Date\" integer, \"Account Name\" varchar(255) not null, \"Total Sold Imps\" integer, \"Imps Bought\" integer, \"Publisher Rev\" numeric)",
function(err, res){
if(err){ console.log("error: " + err); }
else{
db.revenue.insert(data, function (err, res) {
if(err){ console.log("error: " +err); }
done();
});
}
});
}
If the table does not exist it creates it and then exits with:
TypeError: Cannot read property 'insert' of undefined
The second time around (or if the table does exist) it works perfectly.
I also tried using async.series
with the same results.
async.series([createTable, saveTable], function(err,results){
if(err){ console.log("error: " + err); }
done();
});
function createTable(cb){
db.run("CREATE TABLE IF NOT EXISTS revenue(id serial primary key, \"Date\" integer, \"Account Name\" varchar(255) not null, \"Total Sold Imps\" integer, \"Imps Bought\" integer, \"Publisher Rev\" numeric)",
function(err, res){
if(err){ console.log("error: " + err); }
else cb();
});
}
function saveTable(cb) {
db.revenue.save(data, function (err, res) {
if(err){ console.log("error: " +err); }
cb();
});
}
Is there anyway to asynchronously create a table and then use it? Thanks!