Alok
Alok

Reputation: 9

How to create offline database for Phonegap mobile application?

I am creating phonegap mobile app for Android and I need to store questions in offline mode with app. Can I use JSON for this. If yes please guide me how can I store my questions in separate text file and how can I access the specific question in my app from the container text file.

Upvotes: 0

Views: 883

Answers (3)

Krunal
Krunal

Reputation: 327

You can use WebSql database. It doesn't require any additional plugin to install with Phonegap app. http://docs.phonegap.com/en/2.9.0/cordova_storage_storage.md.html#Storage

WebSql database have database size limit of 50MB. So if you want to go with higher storage capacity, you can choose Sqlite database. To use Sqlite databse in Phonegap app, you need Sqlite plugin. https://github.com/litehelpers/Cordova-sqlite-storage

Upvotes: 0

Abhijeet Naik
Abhijeet Naik

Reputation: 1

You can also use android sqlite, you can find phonegap/cordova plugins for that. Sqlite is a lightweight RDBMS.

Let me know if you are facing any issues setting it up.

Abhijeet Naik www.pattypets.com

Upvotes: 0

Shadi Shaaban
Shadi Shaaban

Reputation: 1720

You can use localStorage to store your questions as an array of objects as follows:

questions = [
   { id : 1, text: "Question 1 text goes here" },
   { id : 2, text: "Question 2 text goes here" },
   { id : 3, text: "Question 3 text goes here" },
   { id : 4, text: "Question 4 text goes here" }
];

window.localStorage.setItem("questions" , JSON.stringify(questions));

And then to read it back you use JSON.parse();

questions = JSON.parse( window.localStorage.getItem("questions"));
console.log(questions);

P.S. data stored in localStorage doesn't expire and stays after your phonegap application is closed.

Upvotes: 1

Related Questions