I am inserting a row in sqlite table in phonegap using following function. My row is successfully inserted but after that it gives me error message
function executeQuery(qry_str){
var db = getDatabase();
var response = db.transaction(function(tx){
tx.executeSql(qry_str);
}, successCB, errorCB);
}
function errorCB(){
console.log('query error');
}
function successCB(){
console.log('success');
}
Please check out the mistake I am doing.
Related
I tried to JSON data insert into SqLit database in PhoneGap. I created a table with two columns, like this:
function setup(tx) {
tx.executeSql('DROP TABLE IF EXISTS HEADER_DATA');
tx.executeSql("create table if not exists bookinformation(inserkey TEXT, key TEXT)");
}
This code runs successfully and the table is created. Then, I insert JSON data into the bookinformation table, like this:
function dbReady() {
db.transaction(function(tx) {
alert("5");
$.getJSON('http://echo.jsontest.com/key/value/one/two',function(data){
$.each(data, function(i, dat){
tx.executeSql('INSERT OR REPLACE INTO bookinformation (inserkey, key) VALUES("'+data.one+'", "'+data.key+'")');
alert("completed");
});
});
}, errorHandler, function() { alert('added row'); });
}
However, the insert statement fails. I get this error:
Uncaught InvalidStateError:Failed to execute 'executeSql' on 'SQLTransaction':SQL execution is disallowed
What is causing this error?
Old question but this might help others.
That error is usually caused by the transaction tx being stale.
This is because of the ajax call and by the time your ajax callback gets hit that tx object is no longer valid. The same happens if you use setTimeout or any time consuming non-Websql operation aswell.
To avoid that simply, create the transaction inside in the callback.
E.g
function dbReady() {
$.getJSON('http://echo.jsontest.com/key/value/one/two',function(data) {
db.transaction(function(tx) {
alert("5");
$.each(data, function(i, dat) {
tx.executeSql('INSERT OR REPLACE INTO bookinformation (inserkey, key) VALUES("'+data.one+'", "'+data.key+'")');
});
alert("completed");
}, errorHandler, function() { alert('added row'); });
});
}
I am trying to fetch data from Sqlite database using a sqlite plug-in in phonegap but I seem to be missing something. It doesn't report error when I checked the logcat but instead the database was opened successfully but my code won't fetch the data.
Below is my code so far...
function ViewData(id){
var db = window.sqlitePlugin.openDatabase("mydb", "1.0", "mydb", 100000);
db.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM mytable WHERE userid=?', [id],
function(transaction, result) {
var row = result.rows.length;
if(row>0){
//fetch saved data
alert(result.rows.item(0)['lname']);
}
},errorHandler);
},errorHandler,nullHandler);
return ;
}
Note:
1. The error handler, success handler and null handler are well coded.
Please, any help is appreciated.
Sorry guys, i was calling the function before onDeviceReady
i have an application write with phonegap , i want when first time my application load , tables and data create and insert in database , but when run my application every time , insert data done again , and load time increase , how can chek if table created dont insert data again ?
document.addEventListener("deviceready", onDeviceReady, false);
var dbShell ;
function onDeviceReady() {
dbShell = window.openDatabase("BaharNarenj", "1.0", "BaharNarenj", 2000000);
dbShell.transaction(setupTable,dbErrorHandler,getEntries);
}
function setupTable(tx){
tx.executeSql("CREATE TABLE IF NOT EXISTS amaken(id INTEGER,title,des,px,py)");
tx.executeSql('insert into amaken(id,title,des,px,py) values(2,"test","dec","36.566185","55.059502")');
tx.executeSql('insert into amaken(id,title,des,px,py) values(4,"test5","dec5","36.566185","55.059502")');
}
function dbErrorHandler(err){
alert("DB Error: "+err.message + "\nCode="+err.code);
}
function getEntries() {
alert("done");
}
You can set one flag (like XApp1.0 in following code) in local storage and check that flag's value while subsequent run of the app. Hope this will help you.
function onDeviceReady() {
var firstrun = window.localStorage.getItem("XApp1.0");
if ( firstrun == null ) {
window.localStorage.setItem("XApp1.0", "1");
var db = window.openDatabase("XApp", "1.0", "XApp", 200000);
db.transaction(populateDB, errorCB, successCB);
}
else {
// Db Alredy Exists
var db = window.openDatabase("XApp", "1.0", "XApp", 200000);
db.transaction(queryDB, errorCB);
}
}
I need to use 2 sqlite database for my app
the 2 databases have the some structure because one of it is StoricDB
in some procedure i need to use both db.
i do this code:
var onDeviceReady = function ()
{
testwith2db();
};
document.addEventListener("deviceready", onDeviceReady, true);
function testwith2db (){
var dbCurrent_path = "/sdcard/Android/data/myDatabase/DbCurrent"
var dbStoric_poath = "/sdcard/Android/data/myDatabase/DbStoric"
// this work fine
var db_current = window.sqlitePlugin.openDatabase(dbCurrent_path , "1.0", "DbCurrent", 10000000);
db_current.transaction(doquerycurrent, on_tran_error, on_tran_success);
//this work fine
var db_storic = window.sqlitePlugin.openDatabase(dbStoric_poath , "1.0", "DbStoric", 10000000);
db_storic.transaction(doquerystoric, on_tran_error, on_tran_success);
/*
following lines are my problem
because if i do:... another query on currentdb, the query is alwais executed on last db opened
for me is impossible to use currentdb again
*/
var db_current = window.sqlitePlugin.openDatabase(dbCurrent_path , "1.0", "DbCurrent", 10000000);
db_current.transaction(doquerycurrent, on_tran_error, on_tran_success);
}
function doquerycurrent(txc){
// strange behaviour
// this query is executed on storic db if a have opened it
txc.executeSql("SELECT * FROM table1", [],[]);
}
function doquerystoric(txs){
txs.executeSql("SELECT * FROM table1", [],[]);
}
i have tried with attach db form my query but is unsupported from android ????
tx.executeSql("ATTACH DATABASE '/mnt/sdcard/Android/data/myDatabase/DbStoric' as S",[], on_query_success) // this line return error
please help me
thanks
I am developing an APP using phonegap/coredava while trying to create an access database for the first time after app is installed I am unable to access database but on second run everything working fine how can I fix this my javascript code is below
var dbsize=4*1024;
document.addEventListener("deviceready", onDeviceReady, false);
var dbShell = window.openDatabase("mydb", "1.0", "my db", dbsize);
function onDeviceReady(){
dbShell.transaction(defaultPopulatedb,errorDF,successDF);
}
function defaultPopulatedb(tx){ //creating tables for the first time
tx.executeSql('CREATE TABLE IF NOT EXISTS Userlocation (id INTEGER PRIMARY KEY AUTOINCREMENT, Location TEXT NOT NULL, Locationvalue TEXT NOT NULL)',[],checkfirst,errorTB);
}
function checkfirst(tx)
{
tx.executeSql('SELECT * FROM Userlocation',[],chevals,errorDFS); }
}
function chevals(tx,result)
{
var len =result.rows.length;
if(!len){
tx.executeSql('INSERT INTO Userlocation(Location,Locationvalue) VALUES ("default","default")',[],added,erdf);
}
}
function errorDFS()
{
alert("error");
}
function added()
{
alert("added");
}
function erdf()
{
alert("error adding default");
}
function errorTB()
{
alert("error table");
}
I met this problem once too. You can just simply try{...} catch (ex){...} and ignores the first exception.
Actually, there is a great data access framework for phonegap.