Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Contributor
Avtar_Guleria
Posts: 8
Registered: ‎06-15-2011
My Device: 9800
Accepted Solution

How can i save my Sql Lite Database when created via - db = window.openDatabase("Database", "1.0", "PhoneGapDemo", 65536);

Hello, I am creating a database but each time i compile a new database is created and i have to insert new data each time. The problem is it does not pick the previously created database.

 

How can i save the database in persistent manner? and where this database is actually saved.?

Following is the code for the database creation.

*************************************************************

<Html>
<head>
<script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.1.min.js"></script>

<script type="text/javascript">
  var db;
 
    /*
     *Wait for PhoneGap to load
    */
   
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

   /*
   *PhoneGap is ready
   */
      function onDeviceReady() {
          try {
                if (!window.openDatabase) {
                    alert('Feature not supported');
                } else {
       db = window.openDatabase("Database", "1.0", "PhoneGapDemo", 65536);
       alert("Database Created"+db);
        db.transaction(populateDB, errorCB, successCB);
                }
          }//end try
                catch(e){
                    if (e == 2) {
                        // Version number mismatch.
                        alert("Invalid database version.");
                    }
                    else {
                        alert("Unknown error "+e+".");
                    }

                    return;
                    }//end catch
       
    }//end function
    function alertDismissed(){}
/*
 * Populate the database
 */
    function populateDB(tx) {
       alert("Populating Database");
         tx.executeSql('DROP TABLE IF EXISTS DEMO');
         tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (userid unique, password Text)');
         tx.executeSql('INSERT INTO DEMO (userid, password) VALUES (101, "hrhk")');
         tx.executeSql('INSERT INTO DEMO (userid, password) VALUES (102, "hrhk123")');
         tx.executeSql('INSERT INTO DEMO (userid, password) VALUES (103, "Avtar")');
    }

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

    // Transaction success callback
    function successCB() {
        alert("Data added successfully");
           }
 
    </script>
    </head>
<title> DataBase Example</title>
<body bgcolor="silver" onload="onLoad()">
<center>
<h3> Datbase SCREEN</h3>
<hr color="red">
</center>
</body>
</html>

 

********************************************************

When i insert New Records they are sucessfully inserted and i can see them using select statement But when i recompile the code newly added records are not present.

***********************************************************

Display File.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Details</title>
<script type="text/javascript" charset="utf-8"
    src="phonegap.0.9.5.1.min.js"></script>
<script>
    var db;
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    /*
     *PhoneGap is ready
     */
    function onDeviceReady() {
        try {
            if (!window.openDatabase) {
                alert('Feature not supported');
            } else {
                db = window.openDatabase("Database", "1.0", "PhoneGapDemo",
                        65536);
                alert("Database Opened" + db);

            }
        }//end try
        catch (e) {
            if (e == 2) {
                // Version number mismatch.
                alert("Invalid database version.");
            } else {
                alert("Unknown error " + e + ".");
            }

            return;
        }//end catch

    }//end function

    function display() {
        alert("Display");
        try {
            db.transaction(queryDB, successQueryCB);
        } catch (e) {
            alert("Error in calling display" + e);
        }
    }
    function queryDB(tx) {
        try {
            alert("Inside queryDB()");
            tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);

        } catch (e) {
            alert("Error in extracting Data" + e);
        }

    }
    function querySuccess(tx, results) {
        try {
            alert("Inside query success for select statement");
            // the number of rows returned by the select statement
            alert("Total numbers of rows = " + results.rows.length);

            var html = "<table border=\"2\"><tr><th>User Id</th><th>Password</th></tr> ";
            for (i = 0; i < results.rows.length; i++) 

               html += "<tr><td>"
                        + results.rows.item(i).userid
                        + "</td><td>"
                        + results.rows.item(i).password + "</td></tr>";
            }//end for loop
            document.getElementById("c1").innerHTML = html + "</table>";
        }//end try
        catch (e) {
            alert("Error in select statement result  ::" + e);
        }
    }//end function
    function successQueryCB() {
        alert("Select Statement executed  successfully");
    }
    function errorCB(tx, err) {
        alert("Error processing SQL: " + err);
    }
</script>
</head>
<body bgcolor="silver" onload="onLoad()">
<a href="Insert.html"> Insert Records </a>
<input type="button" value="Display" onclick="display()">

 

<div id="c1"></div>
</body>
</html>

************************************************

Insert.html file

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" charset="utf-8"
    src="phonegap.0.9.5.1.min.js"></script>
<script type="text/javascript">
var db;
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}
/*
 *PhoneGap is ready
 */
function onDeviceReady() {
    try {
        if (!window.openDatabase) {
            alert('Feature not supported');
        } else {
            db = window.openDatabase("Database", "1.0", "PhoneGapDemo",
                    65536);
            //alert("Database Opened" + db);
        }
    }//end try
    catch (e) {
        if (e == 2) {
            // Version number mismatch.
            alert("Invalid database version.");
        } else {
            alert("Unknown error " + e + ".");
        }

       }//end catch
}//end function

function insert(){
    db.transaction(insertRecords,successCB);
 }

 function insertRecords(tx){
        try{
            tx.executeSql('INSERT INTO DEMO (userid, password) VALUES (?, ?)',[insertForm.userid.value,insertForm.password.value]);
            alert("Records Inserted Successfully");
            insertForm.userid.value="";
            insertForm.password.value="";
          }
        catch(e){
            alert("Records Not inserted :"+e);
        }
}

function successCB() {
    alert("INSERT Statement executed  successfully");
}
function errorCB(tx, err) {
    alert("Error processing Insert SQL: " + err);
}

</script>
</head>
<body  bgcolor="silver" onload="onLoad()">
<center>
<h3>Insert Values</h3>
<form name="insertForm">
<hr color="red">
<pre>
User Id:  <input type="text" name="userid"><br>
password: <input type="text" name="password"><br>
<a href="second.html">Back</a>  <input type="button" value="Insert" onclick="insert()">
</pre>
</form>
</center>
</body>
</html>

.....

Thanks in advance.

Please use plain text.
Contributor
aubjf
Posts: 23
Registered: ‎05-11-2011
My Device: 9800 BOLD

Re: How can i save my Sql Lite Database when created via - db = window.openDatabase("Database", "1.0", "PhoneGapDemo", 65536);

Hello Avtar,

 

In my case I ran into a lot of problem with SQL Lite before I use the full kit (7th message) :

http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/creating-or-opening-sqlite-db-in...

 

 

Please use plain text.
New Contributor
Avtar_Guleria
Posts: 8
Registered: ‎06-15-2011
My Device: 9800

Re: How can i save my Sql Lite Database when created via - db = window.openDatabase("Database", "1.0", "PhoneGapDemo", 65536);

Hello, aubjf 

 

Thanks a lot for your reply. But my problem is different

I am inserting the new records and when i click on display button it shows me all the records including the new records. But when the next time i run the application it starts from the begining and does not show the previous record.

 

and also am unable to open the database that was created by the application in sql lite database. it show the error that it is "Not a SqlLite 3 database" and what is the another folder named Local_0 signifies and it contains two files

 

1.)0000000000000001 Database file

2.)0000000000000001.db-journal DB -Journal File.

 

 

Thanks in advance.........

Please use plain text.
Contributor
kam1loherrera
Posts: 19
Registered: ‎06-14-2011
My Device: 9300

Re: How can i save my Sql Lite Database when created via - db = window.openDatabase("Database", "1.0", "PhoneGapDemo", 65536);

You are dropping your table every time the app starts;

 

comment the line  tx.executeSql('DROP TABLE IF EXISTS DEMO'); in populateDB()

 

Regards,

Please use plain text.
New Contributor
Avtar_Guleria
Posts: 8
Registered: ‎06-15-2011
My Device: 9800

Re: How can i save my Sql Lite Database when created via - db = window.openDatabase("Database", "1.0", "PhoneGapDemo", 65536);

Oh,,,yeah...

Really,

Thankyou very much now the problem is solved.

Please use plain text.