2

In my android phonegap application I have written plugin to call native code.In native code I am creating sqlite database,creating the table, and inserting data.Here I am trying to open the same database and fetching the data from javascript file using phonegap storage Api. native android code to open database and create table

enter code here
private void openDatabase(String dbname, String password)
{
File dbfile = this.cordova.getActivity().getDatabasePath("TheTeamScore"+ ".db");
myDB = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
}
private void createTableAndInsert()
{
try {
this.openDatabase(dbname, null);
myDB.execSQL("CREATE TABLE IF NOT EXISTS Contacts(id INTEGER NOT NULL,name TEXT NOT NULL, selectedEmail TEXT, selectedPhone TEXT)");
String sql ="INSERT INTO Contacts (id,name,selectedEmail,selectedPhone) VALUES   (1,'Saranga','Saranga@gmail.com', '7620765783')";
    myDB.execSQL(sql);
}
catch(Exception e)
{
Log.e("Error", "Error", e);
}
}

Javascript file contains the code to open same database(databasename="TheTeamScore") and select data from created table

enter code here
function open_DB() {
return window.openDatabase("TheTeamScore", "1.0", "TheTeamScore DB", 200000);
}
function query_DB(tx) {
if (dbquery == "select"){
tx.executeSql('SELECT * FROM Contacts', [], select_EmailSuccess, error_CBForSelect);
}
}        

but above code is unable to fetch data from Contacts table and showing error no table found Contacts .How to open the same database and use the table using phonegap

Tapaswini
  • 31
  • 2
  • are you aware of the [Phonegap SQLite plugin](https://github.com/ApplicationCraft/PGSQLitePlugin)? – Vlad Stirbu Feb 20 '13 at 13:50
  • @VladStirbu using this plugin we can execute sqlite queries in native java code.But I want to execute the query in my javascript file where ever i need. – Tapaswini Feb 21 '13 at 05:34
  • the implementation is native but the interface is javascript. last time i've used it it has the same api as http://docs.phonegap.com/en/2.4.0/cordova_storage_storage.md.html#Storage – Vlad Stirbu Feb 21 '13 at 07:29
  • this phonegap Api is creating new database.But I want to open the same database that is created using native code.Its like I want to open and use same database and same table in native as well as in javascript.Is it possible ? – Tapaswini Feb 21 '13 at 10:05
  • yes, according to this: http://www.sqlite.org/faq.html#q5 – Vlad Stirbu Feb 21 '13 at 12:02

1 Answers1

0

Both are using different database,in this situation i request you to used following plug-in...

github link

SQLite is a single-user database. If you go the "copy the database" route, then you risk losing data. If the desktop user added data and the iOS user added data, someone's data will be lost when you copy the file.

You can implement your own "syncing" algorithm that identifies changes between the databases and keeps them in sync. This can be a pain, but is a common solution and is what I would recommend. I've done it this way for a desktop/iOS app.

You can choose to use a database server, but this will require network access to connect to it. from here

Community
  • 1
  • 1
Attaullah
  • 3,856
  • 3
  • 48
  • 63