I've been struggling to get this working for too long. I just started to get into PhoneGap/Cordova. I got the demo app running, installed all official plugins, and added this SQLite plugin to begin work with: https://github.com/brodysoft/Cordova-SQLitePlugin
However, I want the plugin to read a pre-packaged database. Simply put, create an app that works offline and does not need to download an SQLite database when it is run for the first time.
I have been developing for Android for quite some time, and the solution was to deploy a read-only database file in the assets folder, and on first run copy the database file into a different folder, where I can read/write as I please.
So I did a little research and realized that it SHOULD be the same with PhoneGap. Place a binary SQLite database somewhere, so that when built, it will be packaged with the app. And then simply load it with the SQLite plugin I linked here.
Finally, the question! What are the exact steps I have to take to be able to read a file that was packaged with the app?
Where should I put my binary file to ensure that it gets packaged into the app?
How exactly should I initialize the File API? Should I ask for persistent storage using window.requestFileSystem? Or should I get some (not sure which) directory (cordova.file.applicationDirectory) as a DirectoryEntry using window.resolveLocalFileSystemURL? If so, what are the EXACT parameters I should use? I am mostly referring to this: https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md
How do I read the packaged file? Using window.resolveLocalFileSystemURL with the right folder? What should then the parameters be?
File copying should work OK if I get to this point. Just copy from the read-only cordova.file.applicationDirectory into cordova.file.dataDirectory, which is read/write. Right?
I know the questions might be pretty basic, but my countless attempts were unsuccessful and I did not find a tutorial that explains this in-depth (believe me, I googled). Also, the documentation seems insufficient.
Just working with a WebSQL database and load data the DB using a s***load of INSERTs is not a viable option, the database has thousands of records.
Thanks for the help in advance, you will save me a headache or two.
Currently Cordova's standard SQLite Plugin supports Pre-populated Databases, which will copy your database file from www and put it into the right directory. It also supports Android as well as iOS, so you don't need to have different logic for different platforms.
When you open the database, you can specify that it is pre-populated using createFromLocation: 1 like this:
var db = window.sqlitePlugin.openDatabase({
name: "my.db",
createFromLocation: 1
});
For iOS, it will first check www and if the file exists, it will copy it to Documents. Keep in mind that it will be backed up by iCloud. If you want to exclude it from iCloud, then add location: 2 on openDatabase as well, which will put your databases in Library/LocalDatabase.
UPDATE (2016)
The original Cordova-sqlite-storage project does not support pre-populated db anymore.
However, this feature just moved to another project from the same creator. Now, use Cordova-sqlite-ext for this purpose.
Related
I'm following this, using js in html to call 'openDatabase("AddressBook",....'. It works, it stores values and calls them them back correctly when I reopen the page but I can't find where the AddressBook DB file is. I've searched my pc, C:\ and appdata etc but I can't find it. I've tried downloading the sqlite3 tool and setting a DB separately and transporting it to the folder but it doesn't seem to recognize it. If I change the name of the DB called it starts a new one and if I then put the previous DB name it shows the stored DB from before. I'd like to know I can locate it and transport it before I learn anymore.
Can anyone advise where it might be?
Is there a way to force it into the same folder as the js files?
That guide seems seems to misunderstand the APIs that it is using. It keeps saying that its using SQLite. It's not. It looks like it is using the deprecated WebSQL spec. The file isn't in your filesystem, it's part of the browser's internal storage and there is no guarantee of how the browser is actually storing it so you can't directly access it from outside the browser.
I'm making the assumption that it is using WebSQL because it mentions using openDatabase and executeSql. WebSQL has been deprecated and is no longer maintained in most browsers. You can get a detailed reasoning from this answer Why is Web SQL database deprecated?. That guide you're following is also 9 years old and doesn't even discuss WebSQL's deprecation.
I want to create an app to estimate engineering costs: lots of tables, forms, a sidebar with a tree structure and so on. You can access a database in the cloud and create table structures according to records in the database.
Naturally that would be a website, however, if the user does not always have an access to the Internet, he/she can download a copy of that database (precisely, a copy of the current version of the database), so that it would be possible to get data from it and work locally. That is why an offline desktop app is needed.
Would it be possible to develop such a hybrid application without first creating a web app and then doing the same with a desktop app?
Previously i have looked into JavaFX, but we saw that it's too difficult to create a website out of that. Then we saw Electron, but i'm not sure if it is the right choice, because it seams that Electron is only used for desktop.
I'm lost as to why you feel you need a website/desktop hybrid. There are many APIs available to let you access a cloud database. All you have to do is find an API with web hooks, or APIs that specifically target the database type you are using (sql, mongo, or whatever). Then just cache a local copy once you've pulled down the database.
Work on the copy then push back to the database when they save, or try to do a push/pull every 5 minutes or something.
You can make a HTML page, which would:
Use AJAX calls if connected
Add relative <script> tag, thinking that HTML file lies in a folder on PC, and that script is somewhere nearbly.
In both cases, user will get same results.
Downloads are:
HTML file with inline script
JS file with database
or
zipped folder with HTML, database JS files and all scripts, images, css, etc. requred for HTML file to show properly.
So I'm building an app using Phonegap and have come across an issue in my ad hoc testing phase.
After installing the app on my iDevices, I've tried to back up the data contained within localStorage to iTunes. For some reason, none of the things I've saved to localStorage seem to be retrieved when I re install the app. I've changed BackupWebStorage in my config file to "local" but that has not helped.
I know that ios 5.1 created many issues for localStorage being backed up and am wondering if that's still the case. If so, what would be my best alternative to localStorage? I'm content with using the File API or a local SQL database, I just need some direction before I rewrite a large amount of code.
Thanks!
I am using Phonegap in Eclipse to make an android app. So I've made an excel file, then I converted it to a db file through using sql lite. Now i have to access that database in javascript. Can anyone give me an idea where to start.
In fact you can access a database that you created through the application Phonegap itself.
You can check the online doc (the link I provide is for Cordova version 1.5.0 but it should be similar for other versions) for more information:
http://docs.phonegap.com/en/1.5.0/phonegap_storage_storage.md.html#SQLResultSet
The problem is that you want to access a database that you pre-propulated. So, the main question about this issue would be: where to put my database file?
I suggest that you read the following links which should answer your questions:
Reading a local sqlite file from phonegap
https://groups.google.com/forum/?fromgroups=#!topic/phonegap/XlenKNxmoqE
I hope this information will help you.
Is it possible to use a Sqlite database in a windows 8 (winRT) javascript application?
What I want to achieve is to download a Sqlite database and store this in local storage before use. I believe some form of local storage is available to javascript based WinRT applications, but I want to know if Sqlite is usable in this scenario.
I'm also aware that the .Net implementation of Sqlite uses some win32 calls and I believe these will not be allowed by the windows8 app cerififcation process.
JavaScript has HTML5 IndexedDB available to it out of the box.
As for SQLite, you can use it, provided that you first wrap it as a WinRT component that can be consumed from JavaScript, e.g. using C++/CX. Its API surface is not that big, so it's certainly possible. I've experimented with compiling SQLite for Metro a while ago, and there were only a few Win32 API calls that were not available in the app container and had to be replaced - nothing major.
We created SQLite3-WinRT for this. It is a WinRT component wrapper for SQLite that passes the Windows metro style app certification. It also includes an easy-to-use JavaScript abstraction.
Take a look at this:
http://sqlwinrt.codeplex.com/
Yes, SQLite database is now supported in Windows 8 RTM. You can now download everything (Windows 8, tools, samples) from Windows Dev Center.
Yes You can use SQLite in WinRT machine.
Just follow following steps
Create a new project.
Go to references , then right click on the references there you will get Manage NuPackage Click on that.
In online search for Sqlite-net package , intall that package.
Again right click on the References and click add refrences , there in Extension section you will get two unchecked reference MS visual C++ run time and Sqlite Windows runtime. check both the reference and add references. It will two CS files in your solution explorer.
Now Download Zip file from https://github.com/doo/SQLite3-WinRT It will provide you the wrapper thing to use cs files in your project.
Unzip at any location.
Now in project go to FILE->ADD->Existing Project-> and browse your unzip location . There you will get a file SQLite3Component.vcxproj inside the SQLite3Component. Add that file.
Now wrapper Project is included in you Project.
Now in your unzipped files you ll get SQLite3JS folder copy that folder and paste in your solution explorer.
Now you are ready to use sqlite in your project.
Try to build your project it will show two error that winres.h file is not there. For that go to error location rename it winresrc.h and build it again. Now it will build and you can use sqlite in your project.
To use .js file of SQLite3JS , Provide the src of js file like SQLite3JS\js\SQLite3.js in html page where you are going to use sqlite.
Here i am giving you some example to use sqlite in js
// This will Create database name db.sqlite and try to create a table Name Notes. If it is not there
var dbPath = Windows.Storage.ApplicationData.current.localFolder.path + '\db.sqlite';
SQLite3JS.openAsync(dbPath)
.then(function (db) {
return db.runAsync('CREATE TABLE Notes (id TEXT PRIMARY KEY, notes TEXT)');
I hope all these step will help you out.