I use phonegap to build applications for Android & IOS.
I store my database using sqlite.
I have a main javascript file in which the initialization of the DB & the main functions are written, then at the other pages I put the database functions related to that page.
The problem is that when I make some operations on the DB from another page then move to the next page & retrieve the data, I find that all what I made before was erased and it just brings the data from the main javascript file.
I'm wondering how can I let the data be saved & prevent it from being deleted whenever I move from one page to another.
The main javascript file that I use at all the pages contains this for the db initialization:
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS Table');
}
Is this the problem ? Should I stop the table from being dropped every time I use the db.
But If I removed it, what should I make at the very first time while creating the DB ?
Yes you every time drop the table so when you move on next page you lost the data from previous page. you should make very first time creating db table like below.
function populateDB(tx) {
tx.executeSql('create table if not exists TableName (id integer)');
}
Related
Hy, I have an iOS application (only for iOS) developed in Titanium Appcelerator using JavaScript.
I'm trying to use SQLite data bases in my app.
I was able to copy my SQLite database to my project with this:
Ti.Database.install('/baseDados/aquarismo', 'AquaInfo');
This "/baseDados/aquarismo" is the database directory inside my project, and "AquaInfo" is the name of the database after copying it.
Then I can open and close my database to get my data from it.
My problem is when I update my SQLite database I need to update it inside my project. So I replace my database inside my project folder with the new database.
But then I need to delete the old database when my app starts and replace it whit the new one.
I have tried this:
var f = Ti.Filesystem.getFile(
Ti.Filesystem.applicationSupportDirectory, 'database','AquaInfo.sql');
//If it's there, delete it
if(f.exists() == true){
f.deleteFile();
}
// Install fresh database
Ti.Database.install('/baseDados/aquarismo','AquaInfo');
However the database doesn't get replaced by the new one.
How can I update my SQLite database when my iOS app starts?
Whenever you update database whether update your table column or create new table and save your changes, new "AquaInfo.sqlite" file generated.
So we have to replace it because we are updating database from external source.
Solution:
If you want database changes at application starting point. You have to write code manually like create table, add column in manual created table and change data type of certain column.
So you can achieve this via manually write code for creating table and related stuff.
I have one method that will insert student attendance record in database. There should be only one attendance record for a student in database for a date. I have to make sure data consistency. I have defined this method as a synchronization method.(ie, Two records should not be inserted for a particular student if we try simultaneously from different system/browser).
public synchronized void SaveOrupdateAttendance(final StudentAttendance studentAttendance) throws ApplicationException {
/*Method Code*/
saveOrUpdate(studentAttendance);
}
Two records are inserted in db. What is wrong with my approach. Please help
synchronisation in JAVA only handle the multi threads to share the resource one by one. It doest mean that the data you use inside the method should not be duplicate .
Have a check before you insert the data into the database or design the application to not allow multiple submission
I am trying to integrate socket.io + node.js + mysql . node-mysql is used to allow node.js to access the MySQL database. A table named tableA in the MySQL database updates with new data every 10 minutes.
Problem: Using node, I need the node server to continuously check tableA for changes whenever tableA gets updated(new row). What is a good way to achieve this?
I am thinking if I used setInterval(checkDb(tableA), 10*60*1000) for fetching all rows and check the changes, the checkDbquery function may not coincide with the database update.
I also happen to be using socket.io so will a good method be for the node system to emit('dbUpdated') from the node server to node client for realtime display of value.
Can anyone help me to do this?
Instead of firing query to fetch all records and then check if anything has changed, you can create a trigger on your tableA. Whenever any operation occurs like insert in your case, just save a timestamp in some other field or other table. So whenever you query after certain intervals you should query this single filed and save this time stamp. And use it to compare it with next timestamp found in query to see if anything has changed.
On app's first load, I retrieve data from storage (Phonegap's WebSQL storage type). Phonegap storage results load fine. Until...
When I go to another page in the app:
Link
If I return back to that main page from the second page using window.location.href in Javascript, I retrieve the WebSQL data again (same function as before).
Phonegap Storage calls the success function (no error), but with an empty result set. It seems to be loading from a second database where I haven't stored anything into (see edit below).
If I force quit app and reopen, storage loads fine again, which shows me that the results in the DB are NOT being deleted.
I load the storage after ondeviceready is fired. What could be wrong?
Note:
1. This is not happening on simulators. only happening on real Android 4.0 device.
2. This app uses jQuery / jQuery Mobile.
function ondeviceready() {
db = window.openDatabase("test", "1.0", "test DB", 2000000);
// . . . //
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM table ORDER BY name', [], querysuccess, function(tx, e) {
errorAlert();
});
}), errorCB;
}
EDIT:
I'm noticing now that if I add a new row when app is first loaded, it stores in one database. Then, if I go to the second page and then back to the first page, and I add another row, it also stores, but in a separate database! (so that's what it seems to be doing). So strange, I see this happening. All my rows are saved and persistent, but the query returns a different group of results depending on whether I went to the second page or not...
Also:
The second page is having a jQuery error for some reason. I just created a blank page with a script link to jQuery, and there is an error. Strange... I wonder if this error is affecting the database? I'm trying to discover how to solve this error.
I've encountered an issue similar to this one before, but my problem was that I was trying to write to the same database from the Java side and the Javascript side. This is how I was able to solve it:
First I ran the app on an Android Simulator and used DDMS to access the databases and that is when I realised that the two were writing in different databases.
I changed from using WebSQL, to using proper SQLite database, and I started getting consistent results after that. It's a drop-in replacement for the phonegap's implementation of database storage, so no need to change your code much. Please see https://github.com/brodyspark/PhoneGap-SQLitePlugin-Android.
I think it's also important to note that WebSQL has some storage limitations, so you might be better off using SQLite that WebSQL. Please see: http://www.steveworkman.com/html5-2/standards/2011/the-limitations-of-websql-and-offline-apps/
Hope this helps :-)
What I've done is I now load the external HTML page through Ajax:
$.mobile.changePage("page2.html");
I also switched to pgsqlite as recommended.
I want to make some forms which will be saved into a PhoneGap database (that one is declared in index.html). Note that the code I will be linking is not index.html, it is another one.
I tried to make the values for these forms to be variables, and then use PhoneGaps javascript to populate the database with these variables.
LINK TO MY CODE
I also want to know this:
* If I build this app, will I be able to store this data and be able to restart my phone and it would still be there? Or will the data be lost if I do so?
* Do I have to define a database on every HTML site or could I somehow make this data on this current HTML-file to be "INSERT" into the database declared in index.html?
Lets try and walk through your questions:
How permanent is data on your phone:
How permanent is local storage on Android and iOS?
Edit.html vs index.html
Did you try and just call the database with this piece of code:
db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
Ofc fill out with your own names that you created your original database with.- Opendatabase either creates the database or opens it.