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
Sets the locale of the database to the the system's current locale. Call
参数pathto database file to open and/or create
factoryan optional factory class that is called to instantiate a cursor when query is called, or null for default
flagsto control database access mode
返回the newly opened database
抛出if the database cannot be opened
我稍微解释一下。这个方法有三个参数
第一个是path,就是sqlite的绝对路径。
第二个关于记录集的
第三个是标记flag,就是打开方式。例如指示是只读打开,读写方式打开等等,还是自动创建的方式打开。其实openOrCreateDatabase就是在这个flag值为CREATE_IF_NECESSARY。具体可以看API说明Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler).
public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory)
Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
public static SQLiteDatabase openOrCreateDatabase (File file, SQLiteDatabase.CursorFactory factory)
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
Flag for
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,
Constant Value: 16 (0x00000010)