[英]Do I have to create all tables at first population of sqlite db?
I'm creating a Sqlite DB & I have many pages that I want to access the db from. 我正在创建一个Sqlite数据库,我有很多页面,我想从中访问数据库。
If I have a new table that I want to create do I have to execute the entire db again ? 如果我有一个我要创建的新表,我是否必须再次执行整个数据库?
And If I have a statement like (Delete, insert, ... ).. Can I just access the db from any where within the code & call the "db.executesql()" whenever I need it? 如果我有一个声明(删除,插入,...)..我可以从代码中的任何位置访问数据库并在需要时调用“db.executesql()”吗?
As PhoneGap's documentation at http://docs.phonegap.com/en/1.9.0/cordova_storage_storage.md.html#Storage says, you can do it this way: 正如http://docs.phonegap.com/en/1.9.0/cordova_storage_storage.md.html#Storage上的 PhoneGap文档所述,您可以这样做:
// Cordova is ready
function onDeviceReady() {
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
// Populate the database
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// Transaction error callback
function errorCB(tx, err) {
alert("Error processing SQL: "+err);
}
// Transaction success callback
function successCB() {
alert("success!");
}
So, you will need to open the database first, and then execute sql, like in the example: 因此,您需要先打开数据库,然后执行sql,如下例所示:
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
For different purposes just create different functions with different sql execution code, just as populateDB
above. 出于不同的目的,只需使用不同的sql执行代码创建不同的函数,就像上面的
populateDB
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.