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
Regular Contributor
Q3test
Posts: 77
Registered: ‎06-13-2013
My Carrier: blackberry cure

How to check table exit or not in db phonegap

Hello 

 

i need to check whether the table exit or not in db.actually i need to add data then i need show data on list liew .In starting i need to show all data which is present on db on listview.but when user come first time there is not table it give error undefined.how to check table exit or not ?

 

here is my code 

http://jsfiddle.net/ravi1989/uQFAS/

 


$(document).ready(function() {

$.mobile.loading('hide');
onDeviceReady();

});

//document.addEventListener("deviceready", onDeviceReady, false);
function doesTableExist(connection:smileyfrustrated:QLConnection, tableName:smileyfrustrated:tring):Boolean
{
connection.loadSchema();
var schema:smileyfrustrated:QLSchemaResult = connection.getSchemaResult();

for each (var table:smileyfrustrated:QLTableSchema in schema.tables)
{
if (table.name.toLowerCase() == tableName.toLowerCase())
{
return true;
}
}
return false;
}
var db = "";
//will create database Dummy_DB or open it

//function will be called when device ready
function onDeviceReady(){
db = window.openDatabase("Casepad", "1.0", "Casepad", 200000);
//db.transaction(queryDB,errorCB);

// db.transaction(populateDB, errorCB, successCB);
}
function insertData(){
db.transaction(populateDB, errorCB, successCB);
}

//create table and insert some record
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS CaseTable (id INTEGER PRIMARY KEY AUTOINCREMENT, CaseName TEXT NOT NULL,CaseDate INTEGER ,TextArea TEXT NOT NULL)');
tx.executeSql('INSERT INTO CaseTable(CaseName,CaseDate,TextArea) VALUES ("'+$('#caseNameValue').val()+'", "'+$('#caseDate').val()+'","'+$('#caseTextArea').val()+'")');
}

//function will be called when an error occurred
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}

//function will be called when process succeed
function successCB() {
alert("success!");
db.transaction(queryDB,errorCB);
}

//select all from SoccerPlayer
function queryDB(tx){
tx.executeSql('SELECT * FROM CaseTable',[],querySuccess,errorCB);
}

function querySuccess(tx,result){
var len = result.rows.length;

for (var i=0; i<len; i++){
$('#folderData').append(
'<li class="row" id="' + i + '">' + '<a href="#">' + '<img src="img/Blue-Folder.png">' + '<h2>'+result.rows.item(i).CaseName+'</h2>' + '<p>'+result.rows.item(i).CaseDate+'</p>' + '<p>'+result.rows.item(i).TextArea+'</p>'+'<span class="ui-li-count">'+ i+'</span></a>' + '</li>'
);
}
$('#folderData').listview('refresh');

}

$("#Cancel").click(function() {
$('.ui-dialog').dialog('close')
$.mobile.changePage($("#Home"), {
transition: "pop",
reverse: false,
changeHash: false
});
});


$("#CancelSettingButton").click(function() {
$.mobile.changePage($("#Home"), {
transition: "slide",
reverse: false,
changeHash: false
});
});

<!--<-------------------Cancel Button Functionality End----------------------------------------->-->
<!-----------------------Add Button Functionality---------------------------------------------------->

$("#AddButton").click(function() {
var isvalid=true;
if(isvalid){
$('#folderData').empty();
insertData();
$.mobile.changePage($("#Home"), {
transition: "slide",
reverse: false,
changeHash: false
});
}
});

Please use plain text.
Administrator
astanley
Posts: 1,359
Registered: ‎07-02-2009
My Carrier: Bell

Re: How to check table exit or not in db phonegap

I'm not able to tell from your code what the issue is.  However if you're looking for a helpful learning resource on how to use WebDB, here is a functional sample page (view source to see how to open, close, read, write, update and delete records):

 

http://blackberry.github.io/WebWorks-Samples/kitchenSink/html/html5/db.html

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,459
Registered: ‎04-12-2010
My Carrier: Bell

Re: How to check table exit or not in db phonegap

Just to add.

 

CREATE TABLE IF NOT EXISTS is a valid way to avoid recreating tables.

 

If you are looking for a query to strictly return whether a table exists or not, you can use something like this:

 

SELECT name FROM sqlite_master WHERE type='table' AND name='MyTable';

 

The result will be empty if it does not exist or it will contain the table if it exists.


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.