Pre-populate iPhone Safari SQLite DB - javascript

I'm working with a PhoneGap app that uses Safari local storage (SQlite DB) via Javascript:
https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/UsingtheJavascriptDatabase/UsingtheJavascriptDatabase.html
On first load, the app creates the database, tables, and populates the data via a series of INSERT statements.
If the user closes the app while this processing is happening, then my app database is left in an inconsistent state.
What I prefer to do is deploy the SQLite DB as part of my iTunes App packaging so nothing must be populated at app cold start. However, I'm not sure if that is possible -- all of the google hits for this topic that I can find are referring to the core-data provided SQLite which is not what we're using...
If it's not possible, could I wrap the entire thing in a transaction and keep re-trying it when the app is restarted?
Failing that, I guess I can create a simple table with one boolean column "is_app_db_loaded?" and set it to true after I've processed all my inserts. But that's really gross...

I think this solution may work for you. which involves pre-creating the table, and copying it over when the app starts up.

Related

How to store data on an iOS web app?

I'm currently working on a planning app, and I've ran into an issue. I can't find a way to store data without it being lost after restarting the app. I've tried cookies, window.name (despite the fact that that shouldn't even work to begin with), but neither of those work. The tasks (planned tasks) will be stored on the server, but seeing it's per person, having in input field for login everytime you start the app doesn't seem like the best plan. Any suggestions?

Store a large set of relational data in a JavaScript Electron Desktop App?

I am building a Bookmarking app using JavaScript and Electron http://electron.atom.io/ to make it a desktop offline app.
Generally I would be building it online as a web app with a PHP and MySQL backend. As a self hosted Electron desktop app I do not have the pleasure of a nice relational database that can handle massive loads of data!
Some info about my data that I need to somehow store on the users computer for the Electron app...
Assuming this was a typical MySQL database it would have these tables...
Bookmark Collection Lists
Bookmark Tags
Bookmarks
website URLs (each URL can have 1 record however a bookmark can have multiple records for the same page as long as they all reference the 1 url record.)
Bookmark Tags relation table to Bookmarks
Bookmarks relation table to website URLs
Potential issues due to size of data collection...
Bookmark Tags table could have anywhere from 1 record up to 5,000 records
Bookmarks and website URL records could easily pass the 10,000 record mark.
My mockup app so far is currently storing the tags and bookmarks in 2 JSON files. Based on the number of records I will have though I do not see the JSON file being a practical solution!
So my question is, what are some possibble solutions to store this amount of relational data for an offline JavaScript Electron desktop app?
My research so far has only resulted in a few JavaScript libraries that act like a database using things like IndexedDB but I am not sure if these will work with my large data set?
Also is there anything relational I can use as I have Bookmmarks which can have multiple Tag records and searching? All articles about using SQLite in Electron have resulted in failure as well as my own attempt at it.
UPDATE
I finally found a working version of SQLite in Electron using SQLJS https://github.com/kripken/sql.js and this test app shows it working E:\Software\ElectronWebApps\electron-sqljs-master
I'm using Localstorage to persist data in my Electron apps. I haven't tried it, but localStorageDB might be what you're looking for.

method that runs only after first install

I am creating an app that get data from the server then save it and work locally with sqlite.
I want to initialize my application with data loaded only after the first installation, saved to sqlite and then loaded from database.
For that i want to create a method that save the data locally, how to make sure this method runs only after the first time the user install the app.
I think that the easier way to do this is to check, at every application run, if your database contains a certain value in a config table: if isn't present, populate the db, otherwise do nothing and continue execution.
In this way you could avoid to write platform specific code (native) since you're doing everything from the js layer.
Obviously I'm supposing that you're already using sqlite in Cordova, and that you can execute sql query/insert from js.

What is good way to store big JSON objects in couchDb?

I work on a web app which store projects data. Data are saved in a couchDb database A. The app pull and push data with a local pouchDb database B, which is sync with A.
So the app can also work offline. When user has connection back, changes made on localDb B during offline time are sent to A using a classic replication.
I store 1 document per project in couchDb, it is a big JSON object with lot of data (project todos, collaborators, advancements, risks, problems, etc...).
It is working like a charm, but I have some problems, and it seems I use pouchDb in wrong way. Situation example:
User A is offline and he adds a todo on project 1.
User B is online and he adds a new collaborator on project 1.
User B changes are pushed to couchDb by the automatic sync.
The project 1 _rev has been incremented.
User B pulls its own changes from couchDb, because the app downloads all documents on any couchDb changes detected. Weird... Idk how to prevent that. But the app still work fine so it's not a big problem.
User A gets its connection back.
User A changes are ignored because of older _rev. But the user did a modification on a different project property, can couchDb detect that himself and merge with newer _rev ?
I clearly see my problem is I'm using 1 document per project. I could use thousands documents to store each properties of each project and my problem woudn't happens, but it seems quite weird: To retrieve all data of a project I would fully scan my database, check document type (collaborator, todos, ...?), and check if the document is linked to the project by adding a new _projectId property to any document.
Currently I just have to request one document, which contains all project data, then I manipulate my JSON easily. It's quite convenient to handle.
How to manage this ? A project may contains averagely 10 to 10 000 properties that multiple users can edit being online or offline.
But the user did a modification on a different project property, can couchDb detect that himself and merge with newer _rev ?
PouchDB/CouchDB conflict handling is described in the PouchDB guide: http://pouchdb.com/guides/conflicts.html
the app downloads all documents on any couchDb changes detected. Weird... Idk how to prevent that.
This is standard PouchDB/CouchDB behavior - you asked it to sync the whole database, so it synced the whole database. :) You can prevent it by using filtered-replication: http://pouchdb.com/api.html#filtered-replication.
How to manage this ? A project may contains averagely 10 to 10 000 properties that multiple users can edit being online or offline.
It really really depends on your data, how frequently it may change, what the unique identifier of a single "property" is... Storing 10,000 separate documents in PouchDB/CouchDB is not a crazy idea, though, and may help you out when it comes to conflicts, since only those individual documents can ever be in conflict.
In general, I'd recommend you read the guide to conflict resolution as described above and review your options. There's also a plugin that may help you with conflict resolution: https://github.com/jo/pouch-resolve-conflicts

Creating my first Mobile App and connecting it to a database

I have to create a Windows Phone 7 app as part of a research project. The app needs to:
Allow the user to login by connecting to an sql server database (stored locally for the purpose of this project)
If login successful, return a list of products associated with the user (i.e. in product table where UserID=x)
User can click on an item in the list, and then add photos for that item. This can be done offline as well (using HTML5 offline storage)
When an internet connection is available again, user can click 'Upload' (or automatically synced, but not necessary) and the images are all uploaded to the Images table with the correct ProductID as Foreign key
Since this is the first time I am doing any mobile programming, I am not sure what is the best approach. I am especially unsure of how I am going to connect to the database. I'm not really interested in learning Silverlight, so the app should use mainly HTML5 and javascript, I also am looking into JQueryMobile.
I have already installed PhoneGap and am trying it out, but my main concern is how to connect the app to the database as I am having trouble finding the 'proper' way of doing this e.g. using some kind of web service or directly through javascript (read it can be done but is not recommended). If anyone could recommend or suggest a good approach of doing this that would be great!
SqlServer is only available on the phone via LinqToSql which you need to expose via a service or a DAL. I dont think that its possible to make calls directly from Javascript to LinqToSql on the phone.
If you're not set on the idea that the DB needs to be on the phone, you can just make service calls via ajax to a server and access the db that way. I recently did a blog post exposing a db via a WCF Data service and consuming it on the phone. It doesn't cover calling it via ajax but should help get you started.
http://www.ritzcovan.com/2012/02/building-a-simple-windows-phone-apppart-4/
If you want to store data within the WP7 database, use the PhoneGap storage APIs.

Categories

Resources