Open prepopulated SQLite database in Cordova (for

2019-08-29 07:24发布

I'm working with Visual Studio 2013 and the Cordova Tools package.

I have most of my app working, and I'm now ready to add the DB logic. I have a DB full of quotes, and I need to tie that into the app.

Following this link: adding dynamic data with SQLite

I was able to get SQLite to talk to a DB (using the sqlitePlugin) However, I can't figure out how to use a pre-populated DB. It always created a new empty DB. I've found several articles on the web that keep saying "put the db in the www folder"... but the Cordova project doesn't have a www folder.

Putting the SQLite DB file in the root doesn't work, and I tried using a path and putting it in a sub-dir. No luck there either.

Has anyone been able to make this work?

2条回答
萌系小妹纸
2楼-- · 2019-08-29 07:34

Old question, but I will answer to serve as a reference: I solved this problem by changing the plugin. Previously, I was using plugin cordova-sqlite-storage (https://github.com/litehelpers/Cordova-sqlite-storage), but it doesn't copy the prepopulated database. It always creates an empty one.

After days of fighting, I uninstalled the plugin and installed this one: cordova-plugin-sqlite (https://www.npmjs.com/package/cordova-plugin-sqlite). This one works like a charm, it will copy the DB in the www folder to the device. Just follow the instructions and don't forget the createFromLocation parameter.

The strange bit it that both plugins are from the same author, and both seem to be active projects. Go figure.

查看更多
欢心
3楼-- · 2019-08-29 07:54

Old question, but I will answer to serve as a reference (2)!

Pre-populated SQLite is supported by cordova-sqlite-ext project.

The steps are simple:

  • put the database file in the www directory and open the database like: www/sample.db

Then, use:

var db = window.sqlitePlugin.openDatabase({name: "sample.db", createFromLocation: 1});

More information: https://github.com/litehelpers/cordova-sqlite-ext#pre-populated-databases

Many sites and answers point to use Cordova-sqlite-storage for this. In fact, this feature was in this project, but it was moved to Cordova-sqlite-ext (they are from the same creator).

I don't recomend using cordova-plugin-sqlite. It is deprecated.

查看更多
登录 后发表回答