accessing phonegap database that was created - android

According to phonegap's API documentation,
window.openDatabase returns a new Database object.
This method will create a new SQL Lite Database and return a Database
object. Use the Database Object to manipulate the data.
However, what I dont understand is what happens when you close the app and reopen it. What happens to the database we created. Isnt it persisted and if so how to we retrieve it in order to perform other operations on it? Any assistance would be appreciated.

Yes, the database is persisted. All you need to do is provide the same "name" to window.openDatabase to open the database you have previously created.

Related

How to delete re-create tables in a SQLite database from Android when an application is started?

I am building an application which has a database with two tables created internally using SQLiteOpenHelper.When ever the application is running,it receives some data and saves it into the tables.What I want is to clear the data tables when ever the application is started?
I looked into this post How can I clear an SQLite database each time I start my application? which is not clear of how to use application.
SQLiteOpenHelper has the ability of creating in-memory databases if you pass the constructor a null name. Probably this is what you are looking for.
For example:
SQLiteOpenHelper sqloh = new SQLiteOpenHelper(context, null, null, 1);
SQLiteDatabase sqldb = sqloh.getWritableDatabase();
will create an in-memory db.
SqliteDatabase uses the method openOrCreate(...) which opens a database if it exist and creates and opens it when it doesn't exist. see docs based on that you could just delete the database file that is created before you do any database calls so that a new one is created each time.
This SO question gives the location of the file: Location of sqlite database on the device
There other route would be just to delete the data in the tables when the application starts by executing a sqlite query:
DELETE FROM your_table
My only thought would be do you really need a database if you are going to delete it every time the application starts. If you are not updating the data then why not just "cache" a json file with the data. The GSON library is awesome for taking json and converting it to java object with very little code, going to be less code than working with sqlite. But the recommendation comes from not having the big picture for what you are trying to accomplish. You then would just delete the json file(s) when the app starts instead of the db file.

Using Database to call objects

I need some advice. I want to implement a set up, which has 4 static saved objects saved in a database. How would a user select one of these objects to use in a service... When using a SQLDatabase, do you have to create a database every time the app starts.
Any advice welcome.
Thanks
When the application starts for the first time the database is created.
The SQLite database saves the information you need into the database, and isnt erased until you allow the user to delete it or update the database.
You can find a very good tutorial on using a database here
EDIT:
It has means for you to allow the user to update the database also, such as information,names,numbers,ect.

What Android sqlite does?

I'm developing a sqlite database in my android application. I need it can grow easily, so I need that in future upgrades, I could change the database, etc.
I want to know if when I change the data base version in the sqlite creation method, it creates a new file of the database. If it does, then, in the onUpgrade I should migrate all the data, isn't it?
In conclusion, what onCreate does exactly? Does it create a new file of the database? Or does it modify the actually one?
I'm asking this because I dont want that the onCreate creates a new file... I want to alter the actual data base only.
Thanks
OnCreate is only called when a database needs to be created for the first time.
OnUpgrade is called if the database already exists and the version numbers do not match. You should use OnUpgrade to alter your database from the old version to the new version.
I am assuming you are referring to OnCreate method in SQLiteOpenHelper. It gets called when you request for a writable or readable database and the the database needs to be created. We do not call the oncreate method directly. So if a database is already existing and we request for a writeable or readable database --- no new database gets created.
Refer to these links for more information
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
http://developer.android.com/resources/tutorials/notepad/index.html

Getting old Dao Object in ormlite

I am using ormlite for sqlite database in my android application. It's a login based application and I have database in SD card. For user abc, the requirement is when user login with different user like xyz then application authenticate the user from the server and server replace the db for user xyz. But when I am trying to access the login credentials it is giving the old credentials while the database is reflecting the new credentials.
I also tried:
DaoManager.clearCache();
It is not working also I tried:
DatabaseManager<DatabaseHelper> manager = new DatabaseManager<DatabaseHelper>();
manager.releaseHelper(DatabaseHelperGenerator.getDataBaseHelperInstance())
After this when I tried to fire this query:
Dao<LoginAuthentication, Integer> loginAuthenticationDao = null;
DatabaseHelperGenerator.getDataBaseHelperInstance().
clearLoginDao(LoginAuthentication.class);
loginAuthenticationDao = DatabaseHelperGenerator.getDataBaseHelperInstance().
getUserDao(LoginAuthentication.class);
List<LoginAuthentication> loginAuthenticationList =
loginAuthenticationDao.queryForAll();
It is giving IllegalStateException :Database not open
Looking for help.
Seems to me that you are going in the wrong direction here.
You are assuming that the DAO is caching objects for you but unless you have enabled an object cache on a particular DAO then that's not the case.
DaoManager.clearCache() doesn't clear object caches but instead clears the cached DAO objects.
Once you release the helper, the database connection is closed which is the reason why you are getting the IllegalStateException.
I'd do some debugging of your application or some logging of values to see where the problem lies. Here are some thoughts:
If you are using an object cache, have you tried to disable it? Have you tried flushing the object cache by calling Dao.clearObjectCache()? Again, a cache will only exist if you have turned it on. It is not on by default.
What code are you using to persist the login information? Anything different than other calls?
How about using the Dao.queryRaw() methods to dump the database right after the insert?
Any transactions?
Best of luck.

SQLite database behaviour in android application development

Hi I am new to android app development and have no idea of SQLite database.
Are The tables we create and the records we enter to the SQLite database tables happening run time. That means once create tables and insert data, do we have to do the same process again when exit and open the app again?(Only the database, tables and records remain until we open the particular app).
Thank you
Anything you do to the database is persistent. It will contain exactly the same data after your app shuts down and is later restarted.
The database and tables will remain after the app exits.
You can create a class that extends SQLiteOpenHelper to help.
Override the onCreate() and onUpgrade() methods there. onCreate() will get called only when your db doesn't exist yet. You can then execute sql to create the tables etc. onUpgrade will get called when you pass a new version number to the super() constructor.

Categories

Resources