Creating a SQLite database in ionic 2 RC0?


#1

I’m trying to follow whatever examples I can find about using SQLite…most are for older versions of ionic. So I’m trying to open the database, but getting an error (in my browser, via ionic serve). So I have a couple questions:

Here’s my code:

  let db = new SQLite();

 db.openDatabase({
      name: "myDB.db",
      location: "default"
  }).then(() => {
      db.executeSql("CREATE TABLE IF NOT EXISTS...<snipped>..)", {}).then((data) => {
          console.log("TABLE CREATED: ", data);
      }, (error) => {
          console.error("Unable to execute sql" + JSON.stringify (error);
      })
  }, (error) => {
      console.error("Unable to open database"+ JSON.stringify(error));  <<<<<<<<<<  I get this error
  });
});

}

  1. my DB doesn’t exist… how do I use the CREATE_IF_NEEDED parameter? I can’t find any examples of passing that into the plugin. Seems that it should be part of the openDatabase (name, location…)

  2. is my problem that I cannot use this code in the browser and I should try the emulator? My emulator won’t start, so I’m kinda at a loss there (it pops up and I see the ANDROID pulsing letters, but the app never runs. )

Thanks,

Bret


#2

You can’t test that in the browser - you need to run it in the emulator or on an actual device.


#3

Thanks, Richard. Are any of the available storage mechanisms capable of “write once, run anywhere”? I.e. write one set of code and you can test it as easily in the browser as you do on a device or emulator? Testing in the browser seems to have shortest development cycle, but if parts of my code won’t run in the browser then you have to start doing more coding and remembering which pieces work where. Ideally I’d be able to pick SqlStorage or Storage or SQLite or PouchDB or something that “just works” everywhere.

Thanks,

Bret


#4

Not sure there will be a solution that works everywhere. See some discussions here about why it was removed from the storage module (it was in there prior to RC0) - Max explains why.


#5

is my problem that i’m run using browser, have any code for this run in browser as
sqlite google chrome…because i’m in build to emulate or run in android device taken lot of time…
i simply ionic serve to check my sqlite data in browser please help me.

thanks…


#6

Hi. Did you get some help because I also need some help here?