1

I'm building a mobile app using Ionic & Cordova and I need to store data in a couple of table in a local db, so I'm using SQLite.

In my app.js I have this:

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);
      });
    }
  });
});

When I run this (cordova run android) on a physical android device, only 1 table is created, favourites, the other location is not.

Any ideas why? Thanks!

4
  • Just tried switching the order of the 2 create statements and weirdly, still only favourites is created. WAT!? Commented Aug 4, 2017 at 15:56
  • Did you notice the error at the end of the 2nd sql query? You need to close the parenthesis: openWeatherId integer'); -> openWeatherId integer)'); Commented Aug 4, 2017 at 16:10
  • Thank you Gustavo - now THAT was a dumb error! Please submit as an answer and I'll mark it correct :) Commented Aug 4, 2017 at 20:29
  • done. 4 eyes see more than 2 :] Commented Aug 6, 2017 at 15:12

1 Answer 1

1

There was a typo in the second SQL query:

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

Closing the parenthesis fixed the problem:

$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS location (id integer primary key, openWeatherId integer)');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.