Page 2 of 2

phonegap database API

Posted: Thu Jul 05, 2012 7:53 am
by adi

HI Max,
the Phonegap API does not work. and when i mean does not work i mean i cannot see the prints of querySuccess function, which suppose to print the DB content.

this is how i invoke the API, this code runs when you click the "Show My Location" button:
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);

this are the public functions:

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")');
}

function errorCB(err) {
alert("Error processing SQL: " + err);
}

function successCB() {
alert("success!");
}

function queryDB(tx) {
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}

function querySuccess(tx, results) {
// this will be empty since no rows were inserted.
alert("Insert ID = " + results.insertId);
// this will be 0 since it is a select statement
alert("Rows Affected = " + results.rowAffected);
// the number of rows returned by the select statement
alert("Insert ID = " + results.rows.length);
}

function errorCB(err) {
alert("Error processing SQL: " + err.code);
}


phonegap database API

Posted: Thu Jul 05, 2012 5:04 pm
by maxkatz

Thanks.. we'll test this. Can you also tell us on which device you tried this?


phonegap database API

Posted: Fri Jul 06, 2012 2:44 am
by maxkatz

Can you share the app for collaboration with a href="mailto:support@tiggzi.com" rel="nofollow"support@tiggzi.com/a?


phonegap database API

Posted: Fri Jul 06, 2012 7:15 am
by adi

Hi Max, shared. device used for testing is samsung S2 android version 4.0.3


phonegap database API

Posted: Fri Jul 06, 2012 6:53 pm
by maxkatz

The problem is in function querySuccess(tx, results), in row

code
alert("Insert ID = " + results.insertId);
/code

From PhoneGap documentation
[quote:]
"The result object has three properties. The first is the insertId which will return the row number of a success SQL insert statement. If the SQL statement is not an insert then the insertId is not set."
[/quote]
http://docs.phonegap.com/en/1.0.0/pho...

In your code, you fetch data from the database via "SELECT" sql statement and then try "results.insertId" property, where an "INVALID_ACCESS_ERR" error occurs.

"results.insertId" property should only be accessed after INSERT SQL statement.


phonegap database API

Posted: Sat Jul 07, 2012 1:47 pm
by adi

Got you Max, thank you!
i just used the coded "as is" from the phonegap API samples, i will revise it now.