2017-08-04 104 views
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台,收藏夹中,其他位置不是。

任何想法为什么?谢谢!

+0

刚试过切换2的顺序创建报表和古怪,仍然只有收藏夹被建造。 WAT!? –

+0

您是否注意到第二次sql查询结束时的错误?你需要关闭括号:openWeatherId integer'); - > openWeatherId integer)'); –

+0

谢谢古斯塔沃 - 现在这是一个愚蠢的错误!请提交作为答案,我会标记它是正确的:) –

回答

1

有没有在第二个SQL查询一个错字:

$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS location (id integer primary key, openWeatherId integer'); 

关闭括号解决了这一问题:

$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS location (id integer primary key, openWeatherId integer)');