How to open an existing WebSQL database?

2020-04-17 06:16发布

问题:

I'm building an HTML5/Phonegap mobile application and I want to use an existing SQLite database through WebSQL. By an "existing" database I mean I had already created the db.sqlite file outside of the app. I did this because there are several tables and it is pre-populated with some data. What I wanna do is copy this db file into my project and be able to open it with Javascript just like this:

var db = openDatabase('mydb', '1.0', 'my first database', 2 * 1024 * 1024);

It seems like this command only creates a new db or opens an existing db that was created with this command. If it's possible to open a DB that was created outside my app, how can I open it? How can I set the db path, filename, etc?

Thanks for any help.

回答1:

WebSQl has that gotcha. Open database without version and then do version migration, eg:

var db = openDatabase('mydb', '', 'my first database', 2 * 1024 * 1024);

if (db.version != '1.0) {
   ...
}