Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Database error

Tags:

sql

cordova

I am trying to set up a database for my phone gap application. the problem is all the transactions apart from the ones setting up the table produce an error saying

"the SQLTransactionCallback was null or threw an exception"

here is the code

function Datasetup()
{
   db=window.openDatabase("PracticeData","1.0","saveData",300000);
   alert("1");
   db.transaction(getDB,onDBError,onDBSuccess);
}
function onDBError(error)
{
    alert("Database Error"+error.message);
}

function onDBSuccess(tx,results)
{
    alert("successfull");
}

function getDB(tx)
{
   alert("2");
   tx.executeSql("CREATE TABLE IF NOT EXISTS session(date,length,activity,pieces)");
   alert("3");
   tx.executeSql("CREATE TABLE IF NOT EXISTS                         pieces(newpiece,name,composer,youtube,images_src,date_Added)");
   alert("4");
   tx.executeSql('SELECT * FROM session', [], onSelectSessionSuccess, onDBError());
   tx.executeSql("SELECT * FROM session",[],onSelectSessionSuccess,onDBError());
   alert("5");
   tx.executeSql("SELECT * FROM pieces",[],onSelectPiecesSuccess,onDBError());
 }

 function savepiece(tx)
 {
    tx.executeSql("INSERT INTO          NOTES(newpiece,name,composer,youtube,images_src,date_Added)VALUES(?,?,?,?,?,?)",[true,pieceData.name,pieceData.composer,"tube","images",date()]);
 }

I don't get the error meseges for this or the create table

       db.transaction(getDB,onDBError,onDBSuccess)

I get error messages for

        tx.executeSql('SELECT * FROM session', [], onSelectSessionSuccess, onDBError());

        tx.executeSql("SELECT * FROM session",[],onSelectSessionSuccess,onDBError());
         alert("5");
        tx.executeSql("SELECT * FROM pieces",[],onSelectPiecesSuccess,onDBError());

that was a great help now one of my selects work the other one however comes up with

"the statement callback raised an exception or statement error callback did not return false"

here's the sql that's not working

     tx.executeSql('SELECT * FROM session', [],onSelectPiecesSuccess, onDBError);

here is the updated code

    function Datasetup(){

db=window.openDatabase("PracticeData","1.0","PracticeData",300000);
db.transaction(getDB,onDBError,onDBSuccess);
    }

    function onDBError(error){
alert("Database Error "+error.message);
    }
            function onDBSuccess(tx,results){
        //db.transaction(query,onDBError);
db.transaction(query,onDBError);
        alert("before");

    }


    function getDB(tx){
//alert("dropping")
//tx.executeSql("DROP TABLE pieces");

tx.executeSql("CREATE TABLE IF NOT EXISTS session(date,length,activity,pieces)");
tx.executeSql("CREATE TABLE IF NOT EXISTS pieces(newpiece,name,composer,youtube,images_src,date_added)");
//tx.executeSql('INSERT INTO session(date, length, activity,pieces) VALUES ("10-2-12", "15","2","11")');
//tx.executeSql('INSERT INTO session (date, length, activity,pieces) VALUES ("11-2-12", "15","2","11")');
//tx.executeSql('INSERT INTO session (date, length, activity,pieces) VALUES ("12-2-12", "15","2","11")');*/
tx.executeSql('INSERT INTO session (date, length, activity,pieces) VALUES ("13-2-12", "15","2","violin")');
tx.executeSql('INSERT INTO pieces (newpiece, name, youtube,images_src,date_Added) VALUES ("true", "15","tube","11",13-9-13)');
//tx.executeSql("DROP TABLE pieces");
//tx.executeSql("DROP TABLE session");
//alert("vi");
    }
            /**sessions**/
            function query(tx){ 
tx.executeSql('SELECT * FROM session', [], onSelectSessionSuccess, onDBError);
    }
    function onSelectSessionSuccess(tx,results){
dbResult = results;
var len= results.rows.length;
var sessionList="";

for(var i=0;i<len;i++)
{
    sessionList = sessionList+"<li>"+results.rows.item(i).date+"</li>"
}
alert(sessionList);
//tx.executeSql("SELECT * FROM pieces",[],onSelectPiecesSuccess,onDBError());
db.transaction(piecesquery,onDBError);
    }

    /**pieces**/
            function piecesquery(tx){
//alert("piecesquery");
tx.executeSql('SELECT * FROM session', [],onSelectPiecesSuccess, onDBError);
    }
    function onSelectPiecesSuccess(tx,results){
var len= results.rows.length;
var PiecesList="";
var newPiecesList="";
var res;
alert(len);
for(var i=0;i<len;i++)
{
    newPiecesList=newPiecesList+"<li>"+results.rows.item(i).newpiece + results.rows.item(i).composer +"</li>"
}
alert(newPiecesList);
$('#newPiecesList').innerHTML(newPiecesList);

    }

my flow: the tables are created and populated getDB. session data is pulled on the success of getDb. pieces data is pulled when the session page initiates. But i can replace the code to pull the pieces table with the code to pull from session table with no problems. for some reason it seems that the data going into the pieces table isn't accessible

like image 332
Nephets Naharnah Avatar asked Jun 15 '26 22:06

Nephets Naharnah


1 Answers

I have done table creation,insert and select process.Hope it will be useful.

var db=null;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {

    db = window.openDatabase("TestDatabase","1.0","TestingDatabase",'200000000');

    alert("db1");
    db.transaction(populateDatabase,errorDb,successDb);


}
 function populateDatabase(tx){

    tx.executeSql('DROP TABLE IF EXISTS TestTable');
    tx.executeSql('CREATE TABLE IF NOT EXISTS TestTable(RollNo INT PRIMARY KEY,FirstName text,LastName text,MobileNo text)');

    tx.executeSql('INSERT INTO TESTTABLE (RollNo, FirstName, LastName, MobileNo) VALUES (1, "Nisari", "Balakrishnan", "8891924207")');
    tx.executeSql('INSERT INTO TESTTABLE (RollNo, FirstName, LastName, MobileNo) VALUES (2, "Mikhil", "Anandan", "9605432101")');

}
function queryDB(tx) {
  tx.executeSql('SELECT * FROM TESTTABLE', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
    var len = results.rows.length;
    alert(len);
    console.log("DEMO table: " + len + " rows found.");
    for (var i=0; i<len; i++){
        //console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
        alert("Row = " + i + " ID = " + results.rows.item(i).RollNo + " FirstName =  " + results.rows.item(i).FirstName + " LastName =  " + results.rows.item(i).LastName + " MobileNo =  " + results.rows.item(i).MobileNo);

//            db = window.openDatabase("TestDatabase","1.0","TestingDatabase",'200000000');
//            db.transaction(updateDB, errorCB);
//            console.log("After Open DB");


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



function errorDb()
{
    alert("Error on Database creation:" + Error);
}
function successDb()
{
    alert("Database is created successfully");
          db = window.openDatabase("TestDatabase","1.0","TestingDatabase",'200000000');
    db.transaction(queryDB, errorCB);
}
like image 87
Nisari Balakrishnan Avatar answered Jun 19 '26 02:06

Nisari Balakrishnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!