android的SDK直接支持sqlite3的API。
打开SD卡上面的sqlite数据库,不需要SQLiteOpenHelper的继承类。只需要,SQLiteDatabase中的一些静态方法。如:
openDatabase(String, CursorFactory, int)
openOrCreateDatabase(File, CursorFactory)
openOrCreateDatabase(String, CursorFactory)
其实openOrCreateDatabase都是调用的openDatabase这个基本的方法。
API的原文描述:
public static SQLiteDatabase openDatabase (String path, SQLiteDatabase.CursorFactory factory, int flags)
引入自: API 级别1Open the database according to the flags
OPEN_READWRITE
OPEN_READONLY
CREATE_IF_NECESSARY
and/orNO_LOCALIZED_COLLATORS
.Sets the locale of the database to the the system's current locale. Call
setLocale(Locale)
if you would like something else.
参数
path to database file to open and/or create factory an optional factory class that is called to instantiate a cursor when query is called, or null for default flags to control database access mode 返回
- the newly opened database
抛出
SQLiteException if the database cannot be opened
我稍微解释一下。这个方法有三个参数
第一个是path,就是sqlite的绝对路径。
第二个关于记录集的
第三个是标记flag,就是打开方式。例如指示是只读打开,读写方式打开等等,还是自动创建的方式打开。其实openOrCreateDatabase就是在这个flag值为CREATE_IF_NECESSARY。具体可以看API说明
public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler)
引入自:API 级别11
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler).
public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory)
引入自:API 级别1
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
public static SQLiteDatabase openOrCreateDatabase (File file, SQLiteDatabase.CursorFactory factory)
引入自:API 级别1
Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
但是我在使用openDatabase这个函数的,flag使用的OPEN_READONLY,发生错误“No such table android_metadata”。
我搜索之后发现在flag这一参数要传入 NO_LOCALIZED_COLLATORS 。看了API,好像是忽略本地化校验。因为我没有android的这方面的背景知识,也没有在网上搜过更详细的解释。所以也没理解透。API对这个常量的原文解释是:
public static final int NO_LOCALIZED_COLLATORS
引入自: API 级别1Flag for
openDatabase(String, SQLiteDatabase.CursorFactory, int)
to open the database without support for localized collators.This causes the collator
LOCALIZED
not to be created. You must be consistent when using this flag to use the setting the database was created with. If this is set,setLocale(Locale)
will do nothing.Constant Value: 16 (0x00000010)