android打開存儲卡(TF卡\SD卡)中的sqlite文件

android的SDK直接支持sqlite3的API。html

 

打開SD卡上面的sqlite數據庫,不須要SQLiteOpenHelper的繼承類。只須要,SQLiteDatabase中的一些靜態方法。如:java

openDatabase(String, CursorFactory, int)android

openOrCreateDatabase(File, CursorFactory)sql

openOrCreateDatabase(String, CursorFactory)數據庫

 

其實openOrCreateDatabase都是調用的openDatabase這個基本的方法。api

API的原文描述:app

public static SQLiteDatabase openDatabase (String path, SQLiteDatabase.CursorFactory factory, int flags)

引入自: API 級別1

Open the database according to the flags OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY and/or NO_LOCALIZED_COLLATORS.ide

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

我稍微解釋一下。這個方法有三個參數ui

第一個是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 級別1

Flag 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)
相關文章
相關標籤/搜索