I have an android application that uses a database created by SQLite Database Browser as shown in this blog: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
First, I created a database with two tables with one of those tables with one entry, just to check if I was getting the database creation correctly. This worked.
After that, I populated one of the tables with 10 more entries, saved, then copied the database to the assets folder (overwriting the previous one), ran the application, and then queried the 2nd - 10th entries. The application was force closing giving me the CursorIndexOutOfBounds error.
From this, I inferred that the application was somehow unable to use the new database I created and was still using the old one (that was overwritten).
Is there a way to get around and/or fix this? The application seems to be using the first version of the database it receives, even if that database is overwritten by a new one.
It sounds like your first DB, the one with only a single entry, still exists in your app's database folder under /data/data/your-package-name (on your android device). That tutorial's code will only copy the database from the assets folder if the database does not exist on the device (look at the checkDataBase() method in the tutorial). So, delete the database from the /data/data/your-package-name (either with code or with root access), make sure your updated DB file is in the assets folder, and then run it again. That will ensure the DB from the assets folder is copied onto the device.
Also, make sure the DB_PATH is equal to context.getApplicationInfo().dataDir + "/databases/" instead of "/data/data/YOUR_PACKAGE/databases/".
Related
In my android studio in data/data/com.../databases there are 3 files .db, shm.db , wal.db and when i save that .db file it is not showing any table.
While in my another mobile phone files there are only two .db, journal.db and it is showing the table and data when we save it .
i don't know why it is happening ???
If you want to check the database contents, you need to downlaod all those 3 files, store them together in a folder and then you would be able to check the contents of your db.
Your problem:
You are only trying to download the .db extension file. Thats why the issue is coming!
On later versions of Android SQLite Write Ahead logging is enabled by default.
Write ahead logging creates the Wal file, see https://sqlite.org/wal.html for more details.
When the database is closed then the contents of the Wal file will be committed to the main DB file, there is also automatic checkpointing and you can trigger it manually.
It is possible to disable it on a per connection basis, but if data does not get moved to the main db file when you application has been closed then your code is not closing the database properly.
I have finished the notepad tutorial on the android site. I did this because i want to create a DB for my app. Once i finished i found out where the data is located in the DDMS. Then..
I downloaded the SQLite browser database i created a mini DB. Know i am trying to put this data into my project.
Can i put this new saved database file(i made with browser)into the notepad database file & and delete the old one. So when the emulator loads it will load my data.
Because i made a database with android notepad with all the code etc. with this SQLite browser i am thinking the code is done for me(table, columns,)
I guess am asking you is can i just make a full database with sql browser and just input it so how in my project.
Your database class needs to be specifc to your database, so you cannot just change databases on the back end and expect the handler class you wrote for the notepad tutorial on the front end to work properly with it.
That being said, you can create a handler to run your custom db. It's rather round-a-bout though. After you get the helper class created, you have to put your db into the assets folder. In your db helper you need code to copy the file out of assets to it's proper home in your applications data directory.
You can find instructions on how to set up your database so the Android framework can use it properly as well as code for copying it out of assets to your data directory here.
I am a novice, and am creating a simple app that just reads data from a table in an SQLite database and displays on the GUI. (Just select operation )
During development, I created the database and the table from the tool "SQLite Database browser". And I inserted all data into the table through the tool.
Now, my doubt is ..
1) In java code, should I have some method that creates database and table ?? (For now, I have methods to do the select operation alone)
2) The database I created is located in my local drive. When the apk file gets created would the database also be included in the apk file ??
Pls help !! Thanks in advance !!
1) In Java code, should I have some method that creates database and table ?? (For now, I have methods to do the select operation alone)
No, You have to just copy your database file from /asset directory to /data/data/<package_name>/database directory. And only use select operation alone. You don't need to use create Database and table operation..
2) The database I created is located in my local drive. When the apk file gets created would the database also be included in the apk file?
For this, as I mentioned above you have to first put your database file into application's /asset directory, then copy it to internal storage (when your application start), then it works.
Look at this SO question How to ship an Android application with a database?. It answered what you needed.
try below link
http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/
I have a problem regarding android database.I have an app which have it's database in assets folder.This app is already installed on the phone and working fine.But now i created another database file(just increase the number of records in the previous database and everything else is same). And put it again in assets folder and replace it with existing database file, and recompile the project. Now in emulator the new database is loading and showing the updated content in database. but when i installed the app again in phone my previous database is showing up and new database is not loading.
But if i uninstall the app from the phone and reinstall it then new database is showing up.
PROBLEM: I just want that if i replace the preexisting app on phone the new database should be loaded, not after uninstalling and then reinstalling the app.
Any suggestion or help?
In Android you must use the onUpgrade function to update your database from a previous version. This is because Android will only create your database once and from then on out it will just use that one. So you must assign a version code to your new database that is different from the old one and use the onUpgrade to actually perform the change to the new database. All devices that are installing from scratch do not have a current database so they are getting the new one created for them.
I bet if you clear the data for your app in Settings and relaunch it, it should load the new database. The reason for this has something to do with SQLiteOpenHelper copying the database from the assets folder in the APK to the application's data directory on the phone if it doesn't already exist. Otherwise it will load the one on the phone. Unless you specifically access the database from the assets folder each time, it will use the cached version.
I have an app which uses a large amount of data which has been compiled outside the app (on my main PC). The app is for my personal use so there are no complications with having to distribute data updates to other users. However, I am currently following a very convoluted and time-consuming procedure each time I want to update the data, and I wonder if anyone can suggest any ways to streamline it.
The procedure I follow whenever I want to add new data is as follows:
I enter the new data into a csv file which I maintain as the source of the relevant table in the database
I use SQLite Database Browser to import the data into an existing SQLite database. (This program does not seem to have the ability to append imported data into an existing table, so whenever a table needs updating I have to delete the existing table, then import data from the csv file into a new table, then manually edit the data types for all the fields in the table.)
I drag the icon for the database file onto the 'assets' folder of my project in Eclipse.
I export the project from Eclipse as an apk file.
I copy the apk file to my phone
(using Astro File Manager) I uninstall the old version of the app and install the new apk.
when the app is run, code based on the example set out here copies the data from the 'assets' folder into the app's data folder; this means that each byte of data takes up two bytes in the phone's internal memory; at present this is not a problem, but could be as the volume of data grows; I wonder if there is a more memory-efficient method?
I should be very grateful for any guidance.
I have found that I can have the SQLite database on the SD card, which simplifies things for me considerably (and reduces the amount of internal memory occupied by my app.)
The first clue was this blog which told me that it is possible (contrary to the impression I had gained from the Android documentation and from my earlier googling) for an app to refer to a database on the SD card.
I then found this page here on Stack Overflow which points out how simple the code can be.
With the help of the advice on these pages, I now have much simpler DBHelper code in my app where the openDataBase() method simply reads as follows:
public void openDataBase() throws SQLException{
File dbfile = new File(DB_PATH + DB_NAME);
myDataBase = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
}
There is no longer any need to include the database in my project's "assets", nor therefore to include it in the apk file. And the great advantage is that the database can be updated without having to go anywhere near Eclipse, let alone reinstalling the app.
I don't really know about the database but I do know that if in eclipse you run the app on your phone it will automatically install it. So instead of running it in the emulator when you press the play button, run it on your phone and many of those steps will be eliminated.
To Wipe Database Clean on startup on the emulator:
Click the down arrow next to the play button in eclipse
Select Run Configurations
Click the Target Tab
Check the "Wipe User Data" check box