Monday, September 17, 2018

Error when reading from pre-populated SQLite database in Ionic project

Leave a Comment

I'm receiving an error when I try to read from a pre-populated SQLite database: sqlite3_prepare_v2 failure: no such table 'plant'

From what I understand SQLite looks for the mydb.db file in the /www folder by default, then creates an empty database when it doesn't find the pre-populated mydb.db file. This is why it can't find the 'plant' table, because the newly created blank database obviously doesn't contain a 'plant' table. However I can confirm that the database is in the /www folder, and that it contains the 'plant' table when I run sqlite3 mydb.db then .tables in the terminal.

I can't figure out why it's not reading from the pre-populated mydb.db file.

Folder structure (from root):

/src -/app --/app.component.ts /www -/mydb.db 

app.component.ts:

  constructor(public platform: Platform, private sqlite: SQLite ) {     platform.ready().then(() => {       this.getData();     });   }    getData() {     this.sqlite.create({       name: 'mydb.db',       location: 'default'     }).then((db: SQLiteObject) => {       db.executeSql('SELECT * FROM plant ORDER BY id ASC', [])       .then(res => {         // Do Stuff       }).catch(e => console.log("FAIL executeSql:", e));     })   } 

I've attempted many fixes that I've found on StackOverflow, like wiping the app from my device, starting a new ionic project then copying app and config files over, and setting a direct path in the database location, but it still keeps trying to read from the empty database that it creates.

1 Answers

Answers 1

The Cordova-sqlite-storage plugin does not handle prepopulated databases (as of this writing). I believe you need the Cordova-sqlite-ext plugin.

From the README.md at the repo (emphasis mine):

New release in September 2018 will include the following major enhancements (litehelpers/Cordova-sqlite-storage#773):

  • browser platform support using kripken / sql.js (litehelpers/Cordova-sqlite-storage#576)

  • cordova-sqlite-storage and cordova-sqlite-ext plugin versions will be combined, no more separate plugin version needed for pre-populated databases (litehelpers/Cordova-sqlite-storage#529)

  • include typings from DefinitelyTyped (litehelpers/Cordova-sqlite-storage#768)

NB. The "will" include is the operative.

There is a similar question on the ionic forum. The last post in the thread starts:

Cordova SQLite Storage doesn’t use your app’s www folder to store data.

Mobile devices don’t allow for app executables’ data to be modified. This is to prevent installing an app that overwrites code in another app (imagine installing WhatsApp and it corrupts all other messaging apps so you can’t use them). It also makes it impossible for code to be injected into the app and take over the OS if your app security isn’t great.

The post goes on with a proposed workaround.

Another thread on the ionic forum seems to indicate that you can use cordova-sqlite-ext in your app. You could search that forum for cordova-sqlite-ext, there are several posters that have it working. There are also many results for cordova-sqlite-ext on this forum that could get you in the right direction.

And to be honest, I do not think it is actually creating an empty database. Unless "/www/mydb.db" is an empty database after you run the app (which would surprise me). While it's true that sqlite will create the database named at the command line if it doesn't exist, I suspect that in this case it is just the behaviour when it cannot access the named database.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment