SQLite data disappears after being updated - android

I've created an empty table in a sqlite database, DB, via a database adapter.
Since I have a lot of data (10,000+ rows) to be inserted and I don't want to do it in the activities so I pull the sqlite database out of data/data/package-name/databases and inserted the rows via sqlite browser software. I then reinserted/push the updated DB it back into the eclipse DDMS and all was fine at first.
The DB has an inflated size of 800kb from the original 22kb.
But after running the emulator again, I noticed that the DB shrunk back to 22kb and upon pulling it out and inspecting it, the rows I inserted is no longer there.
Strange enough, the DB journal file in the same folder is now inflated to 800kb, but I can not open that file (sqlite browser says the file is either corrupt or encrypted but not a valid Sqlite file). Can someone explain what happened?

Did you commit changes after updating database? Also check if database is not dropped when Eclipse uninstall old version of the application before new one is deployed on target.

Related

Change or reset prepackaged database in DBFlow

I am a beginner in android programming, I created an android app which uses an exist DB with DBFlow. At first time I inserted a temp data to database so I can test the app. When I finished building the app I inserted real data and copied the new database -which is similar to the old one except for the real data- to assets folder but I did not find the new data.
How can I reset the database or change the data.
After changing database you must remove previous version of app from your device and then install the app again. This should solve your problem.

clearAllTables doesn't work

Android Room has method void clearAllTables() and according to docs it makes the following:
Deletes all rows from all the tables that are registered to this database as entities().
This does NOT reset the auto-increment value generated by autoGenerate().
After deleting the rows, Room will set a WAL checkpoint and run VACUUM. This means that the data is completely erased. The space will be reclaimed by the system if the amount surpasses the threshold of database file size.
I checked it in my project and looks like db has no data after that call, but when I pulled
*.db file from my device and opened it in sqlite viewer I've seen that all the data exists, the tables are filled and nothing has been erased.
How can that be? I consider this as a potential flaw in my app. Please provide a reliable approach of cleaning room database
looks like db has no data after that call
It means the method worked.
when I pulled *.db file from my device and opened it in sqlite viewer
I've seen that all the data exists
Most probably the transactions are not moved to the original database from the WAL file yet.
Solution
You can force a checkpoint using the wal_checkpoint pragma. Query the following statement against the database.
pragma wal_checkpoint(full)

How can I swap the SQLite file in OrmLite?

I have an android application that relies on a sqlite database, and use OrmLite to access my DB.
Instead of OrmLite creating the tables I rely on downloading the database from a central server as the user will often want to "sync" things. Currently I don't have the fancy sync code written so the app replaces the db. The steps are:
1 Download the latest SQLite db file from the server, as a zip
2 Expand the file to produce a database.sqlite file in a temporary folder
3 Deletes the contents of a data folder, which contains the live database.sqlite file
4 Move the database.sqlite file from the temporary folder to the data folder.
The problem is that the new database file seems to get ignored and DAO queries I run return old data. The only way to show data from the new version of the DB is to restart the application.
To test things I created a table with a timestamp that records when the database was generated, each time you request a new copy of the sqlite db from the server this is updated. I have a fragment that displays this time so you know how fresh your data is. In the fragments onResume method I make a call to the DAO to get the timestamp and put value on screen. I've stepped through this and I see the call to the DAO but the value that comes back is from the old, now deleted, db. Restart the app and the correct value is shown.
So my question is, if I replace the underlying sqlite db file that stores my database, how can I tell ormlite to pick it up or refresh the connection or whatever it has to do???
I tried calling clearObjectCache on the DAO, made no difference.

Malformed SQLite Database Without Exception Being Thrown

I have an SQLite database with records being inserted into it for my Android app.
I am inserting the answers to on screen surveys, with one answer saved per row in my table.
My surveys have 20 questions, and it inserts the answers correctly to the database for the first survey, but then when I insert the answers for the second survey (programatically the same questions, just done again) it malforms the database, but never throws an exception or anything. I run an integrity check before and after and it always returns true.
The unusual thing is that it is only malformed when I try to look at the table on my computer. I tried using the SQLite Database Browser tool, TKSQLite tool, and then opening the DB from the supplied sqlite3.exe, and they all seem to show the DB as malformed. Although the TKSQLite tool notifies me that the table is malformed, I can get the data in other tables. The SQLite Database Browser just shows no data in the table, and sqlite3.exe won't open any tables.
The very unusual thing is if I query the table in the app and print the cursor rows out to log cat, or use the data within the app, all the data is there.
Has anyone seen only partial malformation without an exception being thrown? I would like my database to be pristine...but if my data is there can I hope that it won't get any more malformed?
I worked with SQLite databases for a few projects now doing similar things, and this is the first time I am seeing only partial malformation.
If it will help I am using the Acer a501 tablet programming on Honeycomb 3.2
Additionally, I am accessing the database using a static singleton, to avoid opening and closing the database too often for each insert, and originally inserted via SQL commands using execSQL(), but have also tried using the SQLiteDatabase method insert() to no avail.
We figured out why the database was malforming but not on the device. The tablet we are using is counted as a Portable Media Device according to Windows, and there was some issue when we copied it from the device to the computer. Using a thumb drive to copy to from the device and then from the thumb drive to Windows solved the problem.

Change SQLite Database Version Number

I have a database that I built in SQLite browser, and it works fine. I launched an app with a prebuilt database and now I want to add more tables and data to that database.
I can get the app to launch the onUpgrade() method of the SQLiteOpenHelper. But the problem is, it's doing that EVERY time I use the helper.
I have it localized to, only on app launch, separating the upgrade command from the helper I used to retrieve data, but this is still a problem.
I have figured it out though, as I have been using the same database on my computer (the one that I'm editing) since version 1. So, whenever it writes the newer database onto the SD card it's showing version 1 even though I should be up to version 4 by now.
So, my question is, how can I manually edit the database version of the original database so that when it updates it isn't writing the old version number over the new one?
To manually update the version to 4 you execute the following SQL statement:
PRAGMA user_version = 4
Another way to change the version of your Sqlite Database. You can use DB Browser for SQLite:
Open the database file with "DB Browser for SQLite".
Change the "User Version" number to whatever number you want
Click the "Save" button
You can also set it via the setVersion SqlLiteDatabase method.
Source: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#setVersion(int)

Categories

Resources