How to disable Android SQLite Journal file? - android

I am using transactions to insert multiple rows into table. But, I still see temporary sqlite-journal file being created on SD Card along with sqlite table.
How do I disable creation of journal file?
Thanks

You can elect to keep the journal in memory so that no -journal file is created:
pragma journal_mode=memory;
Note that you need to run this each time you open the database.
I recommend that you read the following to get a better idea of what is going on:
http://www.sqlite.org/pragma.html#pragma_journal_mode

The journal is an internal SQLite file that is used when running transactions (to ensure roll back). You cannot disable it, and you don't want to.

Please remind, the statement PRAGMA journal_mode=OFF or PRAGMA journal_mode=memory is valid per transaction base. You need to set it for every transaction.

You can delete journal file using below code
#Override
public void onOpen(SQLiteDatabase db) {
Cursor cc = db.rawQuery("PRAGMA journal_mode=DELETE",null);
Log.i(TAG,"--- cursor count " + cc.getCount());
super.onOpen(db);
}
You need to use cursor. Cursor is a interface provides random read-write access to the result set returned by a database query.
The journal file will get delete if you use cursor.

Related

How do I force sqlite to merge journal file into main database?

I have an app that writes to the databases with transactions. I understand that sqlite will write to the journal file and then merge it in once the journal file gets to a certain size. I also have functionality that will try and copy the database file to another location. My problem is I can't find a way to force sqlite to merge the journal into the main database.
All of my queries look something like this
SQLiteDatabase database =null;
try{
database = getReadableDatabase();
//PERFORM SOME INSERT
}
catch (Exception e){
Log.e(TAG, "Error with insert");
}
finally {
if( database != null){
database.close();
}
}
After these methods execute I will close the app and inspect the directory where the app saves its database. I can see the database file and database-journal.
I am trying to do something like
public void copyDatabase(){
//FORCE MERGE
//DO OTHER OPERATIONS
}
Does somebody have any ideas on how to merge the journal file when needed?
The journal only contains information about uncommitted transactions. If you are sure the database is not opened from anywhere, the journal file is not in use and it is safe to simply copy the core database file.
Details
Sqlite has multiple journal modes, including DELETE which deletes the journal when the last transaction terminates.
However, the default journal mode under android is PERSIST meaning that the journal file is not automatically deleted by the database engine even when it is not currently in use.
It is possible to change the journal mode in android: Android SQLite - changing journal_mode
db.rawQuery("PRAGMA journal_mode=OFF", null).close();
Note the warning, however:
When using enableWriteAheadLogging(), journal_mode is automatically managed by this class. So, do not set journal_mode using "PRAGMA journal_mode'" statement if your app is using enableWriteAheadLogging()

Is there a memory cache for SQLite in Android and how to release or clear it?

Firstly, I create a database called "mydb" in my Android app:
DBHelper dbHelper = new DBHelper(context, "mydb", null, 1);//DBHelper is my custom class
And write some data into it's table:
SQLiteDatabase db = dbHelper.getReadableDatabase();
db.execSQL("insert into mytable(name, text) values ('allen','hello')");
Here, everything is ok. But then, i delete this database manually not by programming, with a software "R.E. explore" (Certainly on a rooted device).
Then, in my code, i read this table of the database. What is astonishing is that i still could get the data I stored.
Cursor cursor = db.query("mytable", new String[]{"name","text"}, null, null, null, null, null);
Why?
Quoting from the Android Developers reference website:
Once opened successfully, the database is cached, so you can call
this method every time you need to write to the database. (Make sure
to call close() when you no longer need the database.)
This is from the description of the getWritableDatabase() method, however both getReadableDatabase() and getWritableDatabase() return basically the same object for reading the database.
Please note that you should use getWritableDatabase() if you want to persist the changes you make to the database on the device's internal memory. Otherwise they will be valid only for the duration of the application's runtime and will be discarded once the app is closed. If you wish to delete the database completely, you should call the SQLiteDatabase's close() method in order to invalidate the cache.
use SQLiteDatabase.deleteDatabase(File file) API to delete the database
Deletes a database including its journal file and other auxiliary files that may have been created by the database engine.
Make sure you have closed all the connections that are open.
In case you are not able to do that,
just cal the deleteDatabase followed by kill process.. - not recommended
You need to delete the app from your phone then install again

How to Close SQLite Cursor and other open SQLite database?

I am trying to write a code in Android and going through following errors. I am opening a SQLite database and using Cursor.
Java Code
for(int y3=0;y3<10;y3++)
{
String sql_query5= "SELECT * FROM Data WHERE Id="+y3+"";
Cursor cur5 = SQLiteDatabase.openDatabase(db, null, SQLiteDatabase.OPEN_READWRITE).rawQuery(sql_query5, null);
if(cur5.moveToNext())
{
//
}}
So i need to display the Age in the layout but i am facing SQLiteCantOpenDatabaseException and Database object not closed. I don't understand the errors.
Please let me know some solution and suggestion !!!
Do not use hard-coded file paths. You have no guarantees about the structure of the filesystem.
Learn to use SQLiteOpenHelper. This is what helps you create and maintain your SQLite database. Instead of trying to open the database yourself, you create a new SQLiteOpenHelper and call getReadableDatabase().
When you are done reading from a Cursor, call cursor.close().
If you want to close the database, you can call close() on it as well; just be aware that any cursors you got by querying it will no longer be able to give you data, so make sure you are finished them those before closing the database.
add '' in your
String sql_query5= "SELECT * FROM Data WHERE Id='"+y3+"' ";

How can I recreate a database with default values?

In my application, I want to delete my existing database and create a new one with default values. Default values can be inserted to the database from XML.
Does anyone have any idea on how to reuse a database?
Assuming that you are using a SQLite database, to delete all the rows from all of your tables inside your database you can use db.execSQL() and heed the advice from this question Drop all tables command:
You can do it with the following DANGEROUS commands:
PRAGMA writable_schema = 1;
delete from sqlite_master where type = 'table';
PRAGMA writable_schema = 0;
you then want to recover the deleted space with
VACUUM
and a good test to make sure everything is ok
PRAGMA INTEGRITY_CHECK;
If you haven't written a way to read your XML data yet, this is excellent reading: Store parsed xml data to sqlite ? Android
Well basically that's not an Android specific question.
Firstly when do you want to recreate the database with default values and how to trigget it.
In an UI event like button click etc?
Or when you start/stop or destroy your activity?
In any cases you need to drop the database and recreate the whole structure (tables,relationships etc.) again.

Bulk Insertion on Android device

I want to bulk insert about 700 records into the Android database on my next upgrade. What's the most efficient way to do this? From various posts, I know that if I use Insert statements, I should wrap them in a transaction. There's also a post about using your own database, but I need this data to go into my app's standard Android database. Note that this would only be done once per device.
Some ideas:
Put a bunch of SQL statements in a file, read them in a line at a time, and exec the SQL.
Put the data in a CSV file, or JSON, or YAML, or XML, or whatever. Read a line at a time and do db.insert().
Figure out how to do an import and do a single import of the entire file.
Make a sqlite database containing all the records, copy that onto the Android device, and somehow merge the two databases.
[EDIT] Put all the SQL statements in a single file in res/values as one big string. Then read them a line at a time and exec the SQL.
What's the best way? Are there other ways to load data? Are 3 and 4 even possible?
Normally, each time db.insert() is used, SQLite creates a transaction (and resulting journal file in the filesystem), which slows things down.
If you use db.beginTransaction() and db.endTransaction() SQLite creates only a single journal file on the filesystem and then commits all the inserts at the same time, dramatically speeding things up.
Here is some pseudo code from: Batch insert to SQLite database on Android
try
{
db.beginTransaction();
for each record in the list
{
do_some_processing();
if (line represent a valid entry)
{
db.insert(SOME_TABLE, null, SOME_VALUE);
}
some_other_processing();
}
db.setTransactionSuccessful();
}
catch (SQLException e) {}
finally
{
db.endTransaction();
}
If you wish to abort a transaction due to an unexpected error or something, simply db.endTransaction() without first setting the transaction as successful (db.setTransactionSuccessful()).
Another useful method is to use db.inTransaction() (returns true or false) to determine if you are currently in the middle of a transaction.
Documentation here
I've found that for bulk insertions, the (apparently little-used) DatabaseUtils.InsertHelper class is several times faster than using SQLiteDatabase.insert.
Two other optimizations also helped with my app's performance, though they may not be appropriate in all cases:
Don't bind values that are empty or null.
If you can be certain that it's safe to do it, temporarily turning off the database's internal locking can also help performance.
I have a blog post with more details.
This example below will work perfectly
String sql = "INSERT INTO " + DatabaseHelper.TABLE_PRODUCT_LIST
+ " VALUES (?,?,?,?,?,?,?,?,?);";
SQLiteDatabase db = this.getWritableDatabase();
SQLiteStatement statement = db.compileStatement(sql);
db.beginTransaction();
for(int idx=0; idx < Produc_List.size(); idx++) {
statement.clearBindings();
statement.bindLong(1, Produc_List.get(idx).getProduct_id());
statement.bindLong(2, Produc_List.get(idx).getCategory_id());
statement.bindString(3, Produc_List.get(idx).getName());
// statement.bindString(4, Produc_List.get(idx).getBrand());
statement.bindString(5, Produc_List.get(idx).getPrice());
//statement.bindString(6, Produc_List.get(idx).getDiscPrice());
statement.bindString(7, Produc_List.get(idx).getImage());
statement.bindLong(8, Produc_List.get(idx).getLanguage_id());
statement.bindLong(9, Produc_List.get(idx).getPl_rank());
statement.execute();
}
db.setTransactionSuccessful();
db.endTransaction();
Well, my solution for this it kind of weird but works fine...
I compile a large sum of data and insert it in one go (bulk insert?)
I use the db.execSQL(Query) command and I build the "Query" with the following statement...
INSERT INTO yourtable SELECT * FROM (
SELECT 'data1','data2'.... UNION
SELECT 'data1','data2'.... UNION
SELECT 'data1','data2'.... UNION
.
.
.
SELECT 'data1','data2'....
)
The only problem is the building of the query which can be kind of messy.
I hope it helps
I don't believe there is any feasible way to accomplish #3 or #4 on your list.
Of the other solutions you list two that have the datafile contain direct SQL, and the other has the data in a non-SQL format.
All three would work just fine, but the latter suggestion of grabbing the data from a formatted file and building the SQL yourself seems the cleanest. If true batch update capability is added at a later date your datafile is still usable, or at least easily processable into a usable form. Also, creation of the datafile is more straightforward and less error prone. Finally, having the "raw" data would allow import into other data-store formats.
In any case, you should (as you mentioned) wrap the groups of inserts into transactions to avoid the per-row transaction journal creation.

Categories

Resources