新聞中心
Android系統(tǒng)的數(shù)據(jù)庫是應用程序中常用的數(shù)據(jù)存儲方式,而Android系統(tǒng)中的數(shù)據(jù)庫文件可能存在不同的存放位置,本文將為讀者介紹Android數(shù)據(jù)庫存放位置的詳細情況。

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)濱海免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
一、內(nèi)部存儲
1.應用內(nèi)數(shù)據(jù)庫路徑
在Android系統(tǒng)中,每一個應用程序都會有其專屬的內(nèi)部存儲空間,并且每一個應用程序可以有自己的內(nèi)部數(shù)據(jù)庫。在該應用程序中創(chuàng)建的數(shù)據(jù)庫文件將被默認存儲在/data/data//databases/ 目錄下。其中,是應用程序的包名。
2.應用外部存儲路徑
Android系統(tǒng)也支持在外部存儲設(shè)備上創(chuàng)建、讀寫應用程序的數(shù)據(jù)庫文件。這對于需要存儲較大量數(shù)據(jù)的應用程序來說是十分重要的,因為內(nèi)部存儲空間有限。外部的數(shù)據(jù)庫將被存儲在 /mnt/sdcard/Android/data//database/ 目錄下。其中,是應用程序的包名。同時需要在AndroidManifest.xml文件中添加如下權(quán)限:
二、SQLite數(shù)據(jù)庫API
安卓系統(tǒng)中,sqlite數(shù)據(jù)庫API包含4個類:SQLiteOpenHelper、SQLiteDatabase、Cursor和DatabaseUtils。
1.SQLiteOpenHelper
SQLiteOpenHelper可以協(xié)助開發(fā)人員實現(xiàn)數(shù)據(jù)庫的創(chuàng)建和升級。其主要包含了onCreate()、onUpgrade()和getReadableDatabase()、getWritableDatabase()等方法。其中,onCreate()用于在數(shù)據(jù)庫被創(chuàng)建時執(zhí)行初始化操作;onUpgrade()用于在數(shù)據(jù)庫版本更新時執(zhí)行更新操作;getReadableDatabase()和getWritableDatabase()用于獲取可讀可寫的數(shù)據(jù)庫實例。
2.SQLiteDatabase
SQLiteDatabase類是SQLiteOpenHelper的子類,其可以理解為對SQLite數(shù)據(jù)庫的封裝。我們可以使用SQLiteDatabase類對數(shù)據(jù)庫進行創(chuàng)建、升級、查詢、更新和刪除操作。
3.Cursor
Cursor是查詢結(jié)果的封裝。使用Cursor可以方便地獲取查詢結(jié)果中的每一個條目。
4.DatabaseUtils
DatabaseUtils類是一個工具類,主要提供了一些常用的數(shù)據(jù)庫操作。比如將對象傳遞給ContentValues,將Cursor轉(zhuǎn)化為List等操作。
舉例說明,以下是一個創(chuàng)建數(shù)據(jù)庫表的示例。
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DB_NAME = “my_db”;
public static final int DB_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = “CREATE TABLE user (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, sex TEXT)”;
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion
db.execSQL(“DROP TABLE IF EXISTS user”);
onCreate(db);
}
}
}
三、
相關(guān)問題拓展閱讀:
- Android 請問如何更改android 數(shù)據(jù)庫的存儲路徑
- 在Android中使用SQLite,用getWritableDatabase()方法創(chuàng)建后,數(shù)據(jù)庫文件在哪兒?
Android 請問如何更改android 數(shù)據(jù)庫的存儲路徑
These files will be ones that get deleted first when the device runs low on storage. There is no guarantee when these files will be deleted.
但是,更好不要依賴系統(tǒng)來管攜吵理,應該自己設(shè)定一個更大容量,當超出這個值時自己刪除。
Context.getFilesDir(),Context.openFileOutput(String, int),Context.getFileStreamPath(String),Context.getDir(String, int)
/data/data/files
Android支持在SD卡上的應用私有目錄,核隱慶在改握Froyo版本后,通過getExternalFilesDir()可以獲得具體路徑。該路徑依賴與應用的包名,如果你包為hello.file那么SD開上的應用私有目錄為\mnt\sdcard\Android\data\hello.file\files\.
在使用SD卡目錄時,需注意SD卡是否掛載,可通過Environment.getExternalStorageState()方法進行判斷,如果返回值為Envirnment.MEDIA_MOUNTED表示SD卡處于掛載狀態(tài),可以放心使用。
getExternalCacheDir()和getCacheDir()比較
共同點:
files will be deleted when the application is uninstalled
不同點:
1、The platform does not monitor the space available in external storage, and thus will not automatically delete these files. Note that you should be managing the maximum space you will use for these anyway, just like with getCacheDir().
2、External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on Environment for information in the storage state.
3、There is no security enforced with these files. All applications can read and write files placed here.
在Android中使用SQLite,用getWritableDatabase()方法創(chuàng)建后,數(shù)據(jù)庫文件在哪兒?
數(shù)據(jù)庫存放在 /data/data/PACKAGE_NAME/databases 目錄明褲下
你當然可以指定數(shù)據(jù)庫名字,可以將db文件打包在工程里。
private SQLiteDatabase openDatabase() {
try {
// 獲得dictionary.db文件的絕對路徑
String databaseFilename = DATABASE_PATH + “/” + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
// 如果/sdcard/dictionary目錄中存在,創(chuàng)建這個目錄
if (!dir.exists())
dir.mkdir();
// 如果在/sdcard/dictionary目錄中不存在
// dictionary.db文件,則從res\激巖簡raw目錄中復制這個文件到
// SD卡的目錄(/sdcard/棗畝dictionary)
if (!(new File(databaseFilename)).exists()) {
// 獲得封裝dictionary.db文件的InputStream對象
InputStream is = getResources().openRawResource(
R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte buffer = new byte;
int count = 0;
// 開始復制dictionary.db文件
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
// 打開/sdcard/dictionary目錄中的dictionary.db文件
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
} catch (Exception e) {
}
return null;
}
android數(shù)據(jù)庫存放地址的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于android數(shù)據(jù)庫存放地址,Android數(shù)據(jù)庫:存放位置詳解,Android 請問如何更改android 數(shù)據(jù)庫的存儲路徑,在Android中使用SQLite,用getWritableDatabase()方法創(chuàng)建后,數(shù)據(jù)庫文件在哪兒?的信息別忘了在本站進行查找喔。
成都服務(wù)器托管選創(chuàng)新互聯(lián),先上架開通再付費。
創(chuàng)新互聯(lián)(www.cdcxhl.com)專業(yè)-網(wǎng)站建設(shè),軟件開發(fā)老牌服務(wù)商!微信小程序開發(fā),APP開發(fā),網(wǎng)站制作,網(wǎng)站營銷推廣服務(wù)眾多企業(yè)。電話:028-86922220
名稱欄目:Android數(shù)據(jù)庫:存放位置詳解 (android數(shù)據(jù)庫存放地址)
分享路徑:http://www.dlmjj.cn/article/cohdjoh.html


咨詢
建站咨詢
