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.
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 am making a website where users make posts all the posts are stored in a variable i save these variables in local storage and i host the site through dropbox public folder.
Sadly you can only see your posts because there in local storage.
Is there a way i could store that variable in a json file so other users can access it?
E.g.
var dump = "User post 1"
user 2 can see user 1's post and when they submit a post
var dump = "User post 1, User post 2"
I already have it working with multiple posts but i want to be able to share them on multiple computers
maybe the vars value could be stored in a .json or .txt and when a user posts it adds to that file in dropbox?
JavaScript in the browser does not have the ability to write files, making this idea unfortunately an impossible one. Further complicating things, Dropbox (last I checked) only "serves" static pages, meaning you can't use JavaScript to do an HTTP POST to a Dropbox-hosted file.
Alas, I think the answer is, "No, it can't be done like this".
So I'm using Javascript and ajax to connect to a database through an php file, but something came in mind.
If a User log in, the user data will be stored in my Javascript file tittle UserProces.js as:
Var Username = "James"
Var Age ="25";
(Data obtain from a query through a php: RetrieveUserData.php)
If 1 minute after James loged in, another user name Amy log in will the values of name and age of amy will effect the values of James? Since there is only one UserProces.js.
Of course NO! Each user is getting his local copy of javascript file.
The server sends each client that requests the page a copy of the javascript file it has stored. That copy is then in their browser and running there. Any changes to variables are done in that copy in their browser. They have to way (well, unless you set up something special) to change the original file on the server. Think of it like this:
I'm a teacher with a test document on my computer (this is the javascript file on the server). For each student who comes into the class and asks to take the test (a client requesting the page) I'm going to print off a copy in my printer and give them. They will then write their name on the test and fill in answers (assign values to variables). A student doing this doesn't effect anyone else in the class because they aren't changing the original document, they are just editing their copy.
Not a perfect analogy, obviously, but pretty darn close.
Also, addressing a comment made earlier, you probably aren't accessing the service "through a php file". You are using a php file to generate a copy of the web page for the user to view. Again, printing off a copy for the user, but in this case the php file gives a special set of instructions for exactly what should be "printed off".
Each user will load the same script file but all variables, objects and everything else gets stored by each browser, and even your browser doesnt share that info, which prevents one website to have access to variables on another website.
So, final answer is no. They will not share any info. Just load the same "base".
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 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)');
}