views:

523

answers:

1

In some books and online I see these method calls being made after a database is created:

db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(DB_VERSION);

Why is this done? As far as I can tell, after creating a new database, the system adds a table named android_metadata with one field named locale and that table has one row with the locale field set to "en_US". Now I assume the column has that value because I am using a U.S. phone, and if I were using a phone from a different region then the locale field would be set appropriately. Can anyone confirm this? I'm guessing that the setLocale method would only be useful in the case that you install a pre-built database onto a phone and then want to change the locale to match the phone's locale. Sound right?

The documentation for setLockingEnabled says it defaults to true so there's no need to make that call, right?

Lastly, what's with the call to setVersion? I can't find a table that contains this information so I've been assuming that the database file itself stores the version number somewhere. So when I create a database, which requires you to have already specified the version number in the call to the SQLiteOpenHelper constructor, there's no point in calling setVersion. Again, perhaps this method exists for the case of installing a pre-built database to a device and you then wish to change the database's version (though I can't think of when doing this would make sense).

Thanks for any insight!

A: 

You use setLocale() when you have certain content in different languages and you want to do custom order by. Android SDK allows you to call order by using

select........ order by display_name COLLATE LOCALIZED ASC

the emphasis here is on COLLATE LOCALIZED. This will order the column with the locale alphabet.

Pentium10
I get how the locale stuff works. I just don't get why I keep seeing those lines in books and online articles that discuss using a SQLite database on Android. It's like the authors are all doing the same cut-and-paste of that sample code without knowing if its actually necessary or not! As far as I can tell that code is not necessary and just wanted to verify that.
gdoten
Suppose you have a database holding accented text, sure you need the correct order by to get the proper results eg: `as,állat,dig,egg,éden,ivory,work` otherwise you end up with those accented stuff in the end
Pentium10
Yes. But a newly created database already has the locale set, so why would you go and make a call to set the locale to what it is already set to? Same with the locking and version calls--the database has already set those so there's no point in making those calls.
gdoten