So far in my app development (phonegap/JQuery Mobile) I have setup a database and have a table created to which I have implemented a few functions that interact with the table for different events e.g. user registration.
到目前为止,在我的应用程序开发(phonegap/JQuery Mobile)中,我已经设置了一个数据库,并创建了一个表,为此我实现了一些与表交互的功能,例如用户注册。
However, I need to create another table within the same database however I'm not sure how to go about this.
但是,我需要在同一个数据库中创建另一个表,但是我不确定如何进行此操作。
I attempted to created another function (createEvent(tx) {) that creates a table however that isn't working.
我尝试创建另一个函数(createEvent(tx){),该函数创建一个不工作的表。
Please see my javascript below which builds the database and creates the table with.
请参阅下面的javascript,它构建数据库并使用它创建表。
document.addEventListener("deviceready", onDeviceReady, false);
var db;
function onDeviceReady() {
db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2*1024*1024);
db.transaction(createDB, createEvents, errorCB, successCB);
}
function createDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerEarth (UserName text, FirstName text, LastName text, Email text, Password text, CPass text)');
}
function createEvents(tx) {
tx.execute2('CREATE TABLE IF NOT EXISTS SoccerEvents (Title text, Location text, NoPeople text, Date text, Description text)');
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
function successCB() {
alert("Database Ready!");
}
2 个解决方案
#1
2
I would do something like this, just for simplicity:
我想这样做,只是为了简单:
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerEarth (UserName text, FirstName text, LastName text, Email text, Password text, CPass text)');
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerEvents (Title text, Location text, NoPeople text, Date text, Description text)');
}, errorCB, successCB);
#2
0
You can pass the SQL statement as an array of string something. and execute each statement in the array.
可以将SQL语句作为字符串数组传递。并执行数组中的每个语句。
var sql = [ "CREATE TABLE IF NOT EXISTS LEAD (ID VARCHAR(35) PRIMARY KEY NOT NULL, PROCESS VARCHAR (32), VERSION VARCHAR (8), CREATED_BY VARCHAR (128), CREATED_ON VARCHAR (32), RECIPIENT VARCHAR (32), STATUS VARCHAR (8))",
"CREATE TABLE IF NOT EXISTS DOCUMENT (ID INTEGER PRIMARY KEY AUTOINCREMENT, LEAD_ID VARCHAR(35) REFERENCES LEAD (ID) NOT NULL, NAME VARCHAR (255) NOT NULL, TYPE VARCHAR (32) NOT NULL, CONTENT BLOB NOT NULL, RETRY INTEGER NOT NULL, STATUS VARCHAR (8))",
"CREATE TABLE IF NOT EXISTS FUNDATA(ID INTEGER PRIMARY KEY AUTOINCREMENT, FUN_ID INTEGER REFERENCES LEAD (ID), NAME VARCHAR(128) NOT NULL, VALUE VARCHAR (255) NOT NULL)" ];
now pass all the sql to function which would execute , also pass your database object . Callback can be any other function.
现在将执行的所有sql传递给函数,也传递数据库对象。回调可以是任何其他函数。
function write(database, sql){
this.sql = sql;
database.transaction(
function(tx){
for(var i=0; i<this.sql.length; i++){
console.log("execute sql : " + this.sql[i]);
tx.executeSql(this.sql[i]);
}
},function(error){
console.log("error call back : " + JSON.stringify(error));
console.log(error);
},
function(){
console.log("transaction complete call back ");
}
);
}
Hope it provides a better way of doing things and making the code more robust .You could also execute Insert statement like this.
希望它能提供一种更好的方法,使代码更加健壮,您也可以执行这样的Insert语句。
#1
2
I would do something like this, just for simplicity:
我想这样做,只是为了简单:
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerEarth (UserName text, FirstName text, LastName text, Email text, Password text, CPass text)');
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerEvents (Title text, Location text, NoPeople text, Date text, Description text)');
}, errorCB, successCB);
#2
0
You can pass the SQL statement as an array of string something. and execute each statement in the array.
可以将SQL语句作为字符串数组传递。并执行数组中的每个语句。
var sql = [ "CREATE TABLE IF NOT EXISTS LEAD (ID VARCHAR(35) PRIMARY KEY NOT NULL, PROCESS VARCHAR (32), VERSION VARCHAR (8), CREATED_BY VARCHAR (128), CREATED_ON VARCHAR (32), RECIPIENT VARCHAR (32), STATUS VARCHAR (8))",
"CREATE TABLE IF NOT EXISTS DOCUMENT (ID INTEGER PRIMARY KEY AUTOINCREMENT, LEAD_ID VARCHAR(35) REFERENCES LEAD (ID) NOT NULL, NAME VARCHAR (255) NOT NULL, TYPE VARCHAR (32) NOT NULL, CONTENT BLOB NOT NULL, RETRY INTEGER NOT NULL, STATUS VARCHAR (8))",
"CREATE TABLE IF NOT EXISTS FUNDATA(ID INTEGER PRIMARY KEY AUTOINCREMENT, FUN_ID INTEGER REFERENCES LEAD (ID), NAME VARCHAR(128) NOT NULL, VALUE VARCHAR (255) NOT NULL)" ];
now pass all the sql to function which would execute , also pass your database object . Callback can be any other function.
现在将执行的所有sql传递给函数,也传递数据库对象。回调可以是任何其他函数。
function write(database, sql){
this.sql = sql;
database.transaction(
function(tx){
for(var i=0; i<this.sql.length; i++){
console.log("execute sql : " + this.sql[i]);
tx.executeSql(this.sql[i]);
}
},function(error){
console.log("error call back : " + JSON.stringify(error));
console.log(error);
},
function(){
console.log("transaction complete call back ");
}
);
}
Hope it provides a better way of doing things and making the code more robust .You could also execute Insert statement like this.
希望它能提供一种更好的方法,使代码更加健壮,您也可以执行这样的Insert语句。