Reputation: 216
can you please tell me how to check table name exist on database or now .I need to count number of elements in table.But if there is no table exist in db then it will error.?
If there is no table in database (name =caseName_h).Then it will give error .So i need to check that .if there is exist of this table then i need to count the number of elements.? I try like this
db.transaction(function (tx) {
tx.executeSql('PRAGMA table_info(a)', [],
function(tx, results) {
alert("hh");
if (results.rows.length == 0) {
alert("No")
} else {
tx.executeSql('SELECT COUNT(*) FROM a' , [],
function(tx, results) {
alert("Yes");
});
}
});
});
Upvotes: 1
Views: 4924
Reputation: 26
you can also query to the database for checking the existence of the table
tx.executeSql("SELECT name FROM sqlite_master WHERE type='table' AND name='tableName'", [], function (tx, result) {
if (result.rows.length == 0) {
//Your logic if table doesnt exist
}
});
Here if result.rows.length == 0 means the table is not exist otherwise exists.
Upvotes: 1
Reputation: 1647
Check this:
this.db.transaction(
function (tx) {
tx.executeSql("SELECT name FROM sqlite_master WHERE type='table'", [], function (tx, result) {
if (result.rows.length == 1) {
//here are no your tables currently
} else {
}
});
}
);
Upvotes: 2
Reputation: 180070
Execute PRAGMA table_info, and check if you get anything back:
tx.executeSql('PRAGMA table_info(' + caseName_h + ')', [],
function(tx, results) {
if (results.rows.length == 0) {
// no table
} else {
tx.executeSql('SELECT COUNT(*) FROM ' + caseName_h, [],
function(tx, results) {
// ...
});
}
});
Upvotes: 0