1
我正在使用Ionic構建移動應用程序& Cordova和我需要將數據存儲在本地數據庫中的幾個表中,所以我使用的是SQLite。無法使用SQLite在ionic1中創建多個表格
在我app.js我有這樣的:
var db = null;
var weatherApp = angular.module('weather', ['ionic', 'ngCordova']);
weatherApp.run(function($ionicPlatform, $cordovaSQLite, Cities) {
$ionicPlatform.ready(function() {
if(window.cordova) {
db = $cordovaSQLite.openDB({name: 'weather.db', location: 'default'});
create table for storing favourites
$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS favourites (id integer primary key, name text, openWeatherId integer)');
create table for storing the current location of the device
$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS location (id integer primary key, openWeatherId integer');
//also tried
// db.transaction(function (tx) {
// //create table for storing favourites
// tx.executeSql(db, 'CREATE TABLE IF NOT EXISTS favourites (id integer primary key, name text, openWeatherId integer)');
// });
// db.transaction(function (tx) {
// //create table for storing the current location of the device
// tx.executeSql(db, 'CREATE TABLE IF NOT EXISTS location (id integer primary key, openWeatherId integer');
// });
$cordovaSQLite.execute(db, 'SELECT tbl_name FROM sqlite_master WHERE type = "table"').then(function (res) {
var tables = [];
if (res.rows.length > 0) {
for (var i = 0; i < res.rows.length; i++) {
if (res.rows.item(i).tbl_name !== "android_metadata"){
tables.push(res.rows.item(i).tbl_name);
}
}
console.log(tables);
// gives:
// Array(1)
// 0 : "favourites"
} else {
console.log('no tables in db!');
}
}, function (error) {
console.log(error);
});
}
});
});
當我運行一個物理Android設備上這個(科爾多瓦運行Android的),創建僅1臺,收藏夾中,其他位置不是。
任何想法爲什麼?謝謝!
剛試過切換2的順序創建報表和古怪,仍然只有收藏夾被建造。 WAT!? –
您是否注意到第二次sql查詢結束時的錯誤?你需要關閉括號:openWeatherId integer'); - > openWeatherId integer)'); –
謝謝古斯塔沃 - 現在這是一個愚蠢的錯誤!請提交作爲答案,我會標記它是正確的:) –