Cordova/Phonegapp - How to use Local Storage Plugin - javascript

I'm using cordova for the first time to develop apps for android.
i have installed cordova local storage plugin.
But i don't know how to use it(what java-script code is to be used).
What i'm making is a To-do list app,so i need to store the strings or whatever the user inputs.
can anyone please post a short example demonstrating the same.
Thanks in advance.

You don't need localStorage plugin to use simple html localStorage.You can simply save and retrieve value without any plugin.
For saving:
localStorage.setItem("key",value);
for retrieving:
localStorage.getItem("key");
You can save upto 5mb per app with local storage.
You can use web sql too. WEB SQL example link It also does not need any plugin.
Then you can use a bit advanced SQL wrappers. This one needs plugin to be installed. Brodysoft SQLite with complete documentation

Related

Any alternative for JS storage under file protocol?

I am developing a HTML5 game, and would like to share it with file base.
So my friends can play it with a local html file (file://xxx.html).
However, most of the storage are based on the domain, such as localStorage.
I would like to know if there is any alternative to persist the game-progress-save under file-protocol?
I searched that windows.name may be the solution, but it can not store data after closing the browser.
Javascript/HTML Storage Options Under File Protocol (file://)
I also found the FileSystem API (Window.requestFileSystem), but seems only Chrome has implemented it.
Any suggestion?
You can:
Bundle it by using Cordova or Electron
Use Node.js + lite-server
Use some free hosting service, my prefered solution would be Firebase as you would get a free yourGame.web.app domain, certificate and storage. You could set it up in less than an hour. To be honest, this is what I would do as it would be easiest for your friends to access the game too.

Free Image database, behind website

I am working on a University project.
I have developed a test harness, one of the features of the test harness is visual regression testing.
I am developing a basic website, as a means to displays these generated images programmatically.
My original idea was to use a mongodb database, however after some reading many people advise not using mongodb to store images.
I then tried to use google drive and drop box, however they do not facilitate permanent url's to the stored images. Or in the case of dropbox, these urls are random so not programmatic.
I need to find a way to store these images, programmatically store them using a nodeJS script, programmatically access them using JS.
Any ideas?
You can use Amazon S3 for saving and retrieveing images with permanent url's
http://docs.aws.amazon.com/AmazonS3/latest/dev/Welcome.html

Save content to web browser

I'm new to JavaScript and web development. I'm practicing by making a to-do list application. I was wondering how I would go about saving the content of the list that was created into the browser. Right now if I refresh my page or exist the browser it will delete everything. I want to prevent that.
Even that this is a broad question I'll try to help you be asking you to try to use HTML5 web storage.
You can store for example easily in locaStorage any data you want.
You can learn more here http://www.w3schools.com/html/html5_webstorage.asp
It's also quite easy with JS:
// Store
localStorage.myValue = "Text";
// Retrieve
var myValue = localStorage.getItem('myValue');
// Delete
localStorage.removeItem('myValue');
You'll need localStorage to do that.
An example of an application you're working using localStorage can be found here
How to use local storage for JavaScript
You can learn more about localStorage at Mozilla Developer Network

PhoneGap - Read pre-packaged sqlite database

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.

Display data from SQL Lite in Android app using phone gap

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.

Categories

Resources