Page 1 of 1

Sqlite stand alone db .. three collections within.

Posted: Tue May 20, 2014 10:25 pm
by Dave Troxel

I am trying to modify the example code (http://devcenter.appery.io/tutorials/...) to match my db and its collections. Here is the example you provided:

var db = window.openDatabase("Wedding 0.1", "", "Wedding 0.1", 1024*1000);

db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS "Tables"(_objectId TEXT, number TEXT, left TEXT, top TEXT, visible TEXT, _createdAt DATETIME, updatedAt DATETIME)', []);
tx.executeSql('SELECT * FROM "Tables" WHERE "objectId" = "' + value.id + '"', [], function(tx, results){
console.log('rows :' + results.rows.length);
if (results.rows.length == 0){
console.log('Go and add table ' + value.number);
tx.executeSql('INSERT INTO "Tables" (objectId, number, left, top, visible, _createdAt, updatedAt) values (?, ?, ?, ?, ?, ?, ?)', [value.id, value.number, value.left, value.top, value.visible, value.createdAt, value.updatedAt]);
}
});
});

My question is this: How do I add all three collections using this topology? Do I simply repeat the entire script three times with the different collections mapped out in each? Please help.


Sqlite stand alone db .. three collections within.

Posted: Tue May 20, 2014 11:33 pm
by Yurii Orishchuk

Hi Dave.

You can duplicate following code.

And just change "Tables" table name for each part of code.

precode

db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS "Tables"(_objectId TEXT, number TEXT, left TEXT, top TEXT, visible TEXT, _createdAt DATETIME, updatedAt DATETIME)', []);
tx.executeSql('SELECT * FROM "Tables" WHERE "objectId" = "' + value.id + '"', [], function(tx, results){
console.log('rows :' + results.rows.length);
if (results.rows.length == 0){
console.log('Go and add table ' + value.number);
tx.executeSql('INSERT INTO "Tables" (objectId, number, left, top, visible, _createdAt, updatedAt) values (?, ?, ?, ?, ?, ?, ?)', [value.id, value.number, value.left, value.top, value.visible, value.createdAt, value.updatedAt]);
}
});
});

/code/pre

Also you should to implement logic to insert rows into certain table.

Please read more about sql lite here: http://html5doctor.com/introducing-we...

Regards.


Sqlite stand alone db .. three collections within.

Posted: Wed May 21, 2014 12:27 am
by Dave Troxel

Yurii, are the Tables what we call Collections? Are they one in the same? I have two required collections for this purpose.
MedsColl
DocsColl

Do I first replace "Tables" with "MedsColl" and replace the columns listed in the script with the ones from "MedsColl"?
Then, repeat the script replacing "Tables" with "DocsColl" and replace the columns listed in the second run of the script with the ones from "DocsColl"?

Am I understanding this correctly?


Sqlite stand alone db .. three collections within.

Posted: Wed May 21, 2014 1:35 am
by Dave Troxel

Does this look correct?

db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS "MedsColl"(_objectId TEXT, number TEXT, left TEXT, top TEXT, visible TEXT, _createdAt DATETIME, updatedAt DATETIME)', []);
tx.executeSql('SELECT * FROM "MedsColl" WHERE "objectId" = "' + value.id + '"', [], function(tx, results){
console.log('rows :' + results.rows.length);
if (results.rows.length == 0){
console.log('Go and add table ' + value.number);
tx.executeSql('INSERT INTO "MedsColl" (objectId, number, left, top, visible, _createdAt, updatedAt) values (?, ?, ?, ?, ?, ?, ?)', [value.id, value.number, value.left, value.top, value.visible, value.createdAt, value.updatedAt]);
}
});
});
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS "DocsColl"(_objectId TEXT, number TEXT, left TEXT, top TEXT, visible TEXT, _createdAt DATETIME, updatedAt DATETIME)', []);
tx.executeSql('SELECT * FROM "DocsColl" WHERE "objectId" = "' + value.id + '"', [], function(tx, results){
console.log('rows :' + results.rows.length);
if (results.rows.length == 0){
console.log('Go and add table ' + value.number);
tx.executeSql('INSERT INTO "DocsColl" (objectId, number, left, top, visible, _createdAt, updatedAt) values (?, ?, ?, ?, ?, ?, ?)', [value.id, value.number, value.left, value.top, value.visible, value.createdAt, value.updatedAt]);
}
});
});


Sqlite stand alone db .. three collections within.

Posted: Wed May 21, 2014 2:03 pm
by Evgene Karachevtsev

Hello Dave,

There are good examples, that might be helpful: http://docs.phonegap.com/en/3.0.0/cor...