SQLitePlugin with cordova and ANDROID - android

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

Related

Visual Studio 2015 apache cordova

I have an app that uses WebSQL transactions to create a table, insert data, and retrieve information from a database.
The app works fine on the Ripple Nexus (Galaxy) but not on the device (Galaxy Note 5) nor the emulator. The UI is there: text boxes, labels, buttons, etc. However, according to my notice, the operations on the database are not performed.
What can I do?
There are not enough details here to tell what is going on and no code samples. Note that ripple is using http and your app on the device is probably using a non http:// calling location. Another possible issue is that on your machine there is web connectivity to a local http endpoint and on the device the end point would not be available unless it is public on the internet / available to the device.
Here is sample of my code, open database and populate data tables:
document.addEventListener('deviceready', onDeviceReady.bind(this), false);
var db = openDatabase('DUTIESDB', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql("DROP TABLE DUTIES", []);
tx.executeSql("DROP TABLE SDUTIES", []);
tx.executeSql('CREATE TABLE IF NOT EXISTS DUTIES (id, name, date, time, course, room)');
tx.executeSql('CREATE TABLE IF NOT EXISTS SDUTIES (id, name, date, time, course, room, invg)');
});
PupulateSupTable();
function PopulateSupTable() {
var xmlhttp = new XMLHttpRequest();
var url = "/scripts/SupDuties.txt";
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
db.transaction(
function (tx) {
var k, sql = 'INSERT INTO SDUTIES (id, name, date, time, course, room, invg) VALUES (?,?,?,?,?,?,?)';
for (k = 0; k < myArr.length; k++) {
tx.executeSql(sql, [myArr[k].ID, myArr[k].Name, myArr[k].Date, myArr[k].Time, myArr[k].Course, myArr[k].Room, myArr[k].Invigilator]);
}
}
);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}

Fetch Data from Sqlite Database using Sqlite Plug-in (PhoneGap)

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

Query executes successfully but still error persists

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.

insert data in sqlite database in phonegap when first time load application

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);
}
}

PhoneGap - How to query SQLite in a for loop?

I have a problem with a SQLite query on PhoneGap application.
I would open a folder and make a select with where clause for every entries in the folder.
I open a for loop, after a select query, but the script uses same variables for all the entries (it remembers only the last record's variables).
The loop reads correctly the number of entries, but it works only with one record's variables, and it works for as many times as there are files in the folder only with these variables.
function readerSuccess(entries) {
var db = window.openDatabase("Test", "1.0", "Demo test", 200000);
db.transaction(function(tx) {
var i;
for (i=0; i<entries.length; i+=1) {
var imgName=entries[i].name;
var upfi=entries[i];
console.log(imgName);
tx.executeSql("SELECT * FROM FOTO WHERE img=?", [imgName], function(tx, result) {
console.log(imgName+" after SQL");
var dataset = result.rows;
console.log("Returned rows = " + dataset.length);
if (dataset.length==0) {
console.log('No rows!');
console.log('File sync: '+imgName);
} else {
console.log('File already sync: '+imgName);
};
}, successCB, errorCB)}}, successCB, errorCB);
};
I have tested with global variables or local variables without a success.
I do not know where to turn!! :(

Categories

Resources