问题描述
我需要为我的应用程序创建一个SQLite DB。我需要以几种欧洲语言存储文本,因此会有大量的重音字符和其他奇怪的标记。我正在扩展 SQLiteOpenHelper
。
I need to create a SQLite DB for my application. I'll need to store text in several European languages, so there will be plenty of accented characters and other weird marks. I'm extending SQLiteOpenHelper
.
检查.db文件我注意到有一个名为<$ c $的额外表C> android_metadata 。有一个名为 locale
的列,在我的模拟器中默认设置为en_US。
Inspecting the .db file I noticed there's an extra table named android_metadata
. There's a single column named locale
, which is set to "en_US" by default in my simulator.
我'已经开发了开发人员指南中的SQLite部分,以及 SQLiteOpenHelper
和 SQLiteDatabase
的javadoc,在SO和谷歌,但我无处可以找到将语言环境设置到数据库的正确位置,或者它是否真的是必要的。猜测它应该在数据库创建时完成,我尝试在帮助程序的 onCreate
方法中调用 db.setLocale
,但我' m,得到此异常:
I've readed the SQLite section in the developer guide, and also the javadocs for SQLiteOpenHelper
and SQLiteDatabase
, searched in SO and in Google, but nowhere I could find what is the correct place to set the locale to the DB, or if it is really neccesary. Guessing it should be done at DB creation, I tried calling db.setLocale
in the helper's onCreate
method, but I'm, getting this exception:
BEGIN TRANSACTION failed setting locale
FATAL EXCEPTION: Thread-9
android.database.sqlite.SQLiteException: cannot start a transaction within a transaction
at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1950)
这是我的方法的样子:
public class MyOpenHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
db.setLocale(new Locale("en","EN"));
...
}
...
}
以下是我的问题:
- 我真的需要将语言环境设置为DB吗?我真的不需要开箱即用的查询,因为我总是可以自己对结果进行排序。
- 我应该在哪里调用
setLocale
?
- Do I really need to set the locale to the DB? I don't really need to get the queries sorted out of the box, as I can always sort the results myself later.
- Where should I call
setLocale
?
推荐答案
- 你需要仅当您打算使用
LOCALIZED
整理算法(取决于系统区域设置)时才设置区域设置 - 如您所述回答,在。
- You'll need to set the locale only if you intend to use the
LOCALIZED
collation algorithm, which depends on the system locale, - As mentioned in your answer, call
setLocale()
ononConfigure()
.
这篇关于为什么以及在哪里调用setLocale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!