javascript - 无法使用 SQLite 在 ionic1 中创建多个表

标签 javascript android sqlite cordova ionic-framework

我正在使用 Ionic 和 Cordova 构建一个移动应用程序,我需要将数据存储在本地数据库的几个表中,所以我正在使用 SQLite。

在我的 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);
      });
    }
  });
});

当我在物理安卓设备上运行这个(cordova run android)时,只创建了一个表,收藏夹,其他位置没有。

有什么想法吗?谢谢!

最佳答案

第二个 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)');

关于javascript - 无法使用 SQLite 在 ionic1 中创建多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45510926/

相关文章:

Android 默认菜单按钮不可见

Android类图

google-app-engine - GAE : How to use unicode characters in url

javascript - 每次发生更改时都使用 jQuery 更新 ruby​​ on Rails 应用程序

javascript - Discord.js 删除单条消息

javascript - amCharts:禁用图例中某些项目的切换

javascript - 字体到跨度转换器

android - 使用 ViewHolder 的 ListActivty 的 BaseAdapter.getView() - Android

sql - 如何在 SQLite 的 varchar 列中选择带有\u0000(空字符)的行?

SQLite 选择所有列都不为空的位置