Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement database in PhoneGap App ?

I'm new to this, and I am trying to build an app to be deployed with PhoneGap, to both Android and iOS. I am wondering how to link to a database which will store timetable data.

My question is how to include the database so it can be packaged with PhoneGap.

I have looked at the PhoneGap docs, and they don't really make sense to me as to how to set up/create the database


Update: This website shows some info on local storage, but when i put it in, hangs on the loading image.

Any further ideas?

http://www.aquim.com/web-article-237.html

like image 356
Mathias Avatar asked May 04 '13 00:05

Mathias


2 Answers

Please refer below link for simple operation with Sq-lite.and also you can get basic idea of Storage API from above link.

Simple operation with Sq-lite : http://www.raymondcamden.com/index.cfm/2011/10/20/Example-of-PhoneGaps-Database-Support

Edited on 8th MAY 2013 and fixed 19th January 2016

Basic operation with DB :

<script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
function onDeviceReady() {
    var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
    db.transaction(populateDB, errorCB, successCB);
}

// Populate the database 
function populateDB(tx) {
    tx.executeSql('DROP TABLE IF EXISTS DEMO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}

// Transaction error callback
function errorCB(err) {
    alert("Error processing SQL: " + err);
}

// Transaction success callback
function successCB() {
    alert("success!");
}
</script>

refrence

You can check Data base in File explorer

In ADT bundle Window>>show view>> File Explorer

like image 175
MSTdev Avatar answered Oct 22 '22 21:10

MSTdev


PhoneGap has a storage api that you should use instead of using HTML5 local storage directly. On both Android and iOS, it will use the native implementation.

see http://docs.phonegap.com/en/2.7.0/cordova_storage_storage.md.html#Storage

like image 25
GreyBeardedGeek Avatar answered Oct 22 '22 21:10

GreyBeardedGeek