ionic sqlite 创建多个表
ionic sqlite create multiple tables
使用 IONIC 3 和 sqlite3 v2 native,在 Android 设备上测试。
当我使用一个 table 时,一切都很好,但是当我尝试创建 3 个 table 时,其他两个就出现问题了。
database.ts(在提供商中):
...
db: any;
isOpen: Boolean = false;
constructor(private sqlite: SQLite) {}
createDatabase() {
return new Promise((resolve, reject) => {
this.sqlite.create({
name: 'mydatabase.db',
location: 'default'
}).then((db: SQLiteObject) => {
this.isOpen = true;
resolve(db);
}).catch(err => reject(err));
});
}
openDb() {
return new Promise((resolve, reject) => {
if (this.isOpen) {
resolve({});
} else {
this.createDatabase().then(db => {
this.db = db;
this.db.executeSql("CREATE TABLE IF NOT EXISTS first (*my first query...)",{})
.then(("CREATE TABLE IF NOT EXISTS second (*my second query...))"),{})
.then(("CREATE TABLE IF NOT EXISTS third (*my third query...)"),{})
.then(res => resolve(res))
.catch(err=>reject(err));
}).catch(err => reject(err));
}
});
}
错误信息:
sqlite3_prepare_v2 failure: no such table: second, code 5
第二次尝试:
openDb() {
return new Promise((resolve, reject) => {
this.createDatabase().then(db => {
this.db = db;
this.db.executeSql("CREATE TABLE IF NOT EXISTS first (firstquery...)",{})
.then(()=>{this.db.executeSql("CREATE TABLE IF NOT EXISTS second (secondquery...)",{})})
.then(res=>resolve(res))
.catch(err=>reject(err));
});
第二个然后从未触发,只有第一个 table 创建。
我用了很多方法,但上面这个似乎是最接近我的问题的解决方案。
这种方法看起来是否正确,或者我必须使用不同的东西才能使它起作用?
什么 executeSql returns 是一个 promise 告诉给定的查询是否成功执行。如果您希望仅在创建第一个 table 时创建另一个 table,那么您需要按照以下步骤进行操作
this.db.executeSql("CREATE TABLE IF NOT EXISTS first (*my first query...)",{})
.then(() =>{
this.db.executeSql("CREATE TABLE IF NOT EXISTS second (*my second query...))"),{}).then(()=>{
//Something else if required
}).catch(err => reject(err));
}).catch(err => reject(err));
使用 IONIC 3 和 sqlite3 v2 native,在 Android 设备上测试。
当我使用一个 table 时,一切都很好,但是当我尝试创建 3 个 table 时,其他两个就出现问题了。
database.ts(在提供商中):
...
db: any;
isOpen: Boolean = false;
constructor(private sqlite: SQLite) {}
createDatabase() {
return new Promise((resolve, reject) => {
this.sqlite.create({
name: 'mydatabase.db',
location: 'default'
}).then((db: SQLiteObject) => {
this.isOpen = true;
resolve(db);
}).catch(err => reject(err));
});
}
openDb() {
return new Promise((resolve, reject) => {
if (this.isOpen) {
resolve({});
} else {
this.createDatabase().then(db => {
this.db = db;
this.db.executeSql("CREATE TABLE IF NOT EXISTS first (*my first query...)",{})
.then(("CREATE TABLE IF NOT EXISTS second (*my second query...))"),{})
.then(("CREATE TABLE IF NOT EXISTS third (*my third query...)"),{})
.then(res => resolve(res))
.catch(err=>reject(err));
}).catch(err => reject(err));
}
});
}
错误信息:
sqlite3_prepare_v2 failure: no such table: second, code 5
第二次尝试:
openDb() {
return new Promise((resolve, reject) => {
this.createDatabase().then(db => {
this.db = db;
this.db.executeSql("CREATE TABLE IF NOT EXISTS first (firstquery...)",{})
.then(()=>{this.db.executeSql("CREATE TABLE IF NOT EXISTS second (secondquery...)",{})})
.then(res=>resolve(res))
.catch(err=>reject(err));
});
第二个然后从未触发,只有第一个 table 创建。
我用了很多方法,但上面这个似乎是最接近我的问题的解决方案。 这种方法看起来是否正确,或者我必须使用不同的东西才能使它起作用?
什么 executeSql returns 是一个 promise 告诉给定的查询是否成功执行。如果您希望仅在创建第一个 table 时创建另一个 table,那么您需要按照以下步骤进行操作
this.db.executeSql("CREATE TABLE IF NOT EXISTS first (*my first query...)",{})
.then(() =>{
this.db.executeSql("CREATE TABLE IF NOT EXISTS second (*my second query...))"),{}).then(()=>{
//Something else if required
}).catch(err => reject(err));
}).catch(err => reject(err));