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

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)濱海免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
一、內(nèi)部存儲(chǔ)
1.應(yīng)用內(nèi)數(shù)據(jù)庫(kù)路徑
在Android系統(tǒng)中,每一個(gè)應(yīng)用程序都會(huì)有其專屬的內(nèi)部存儲(chǔ)空間,并且每一個(gè)應(yīng)用程序可以有自己的內(nèi)部數(shù)據(jù)庫(kù)。在該應(yīng)用程序中創(chuàng)建的數(shù)據(jù)庫(kù)文件將被默認(rèn)存儲(chǔ)在/data/data//databases/ 目錄下。其中,是應(yīng)用程序的包名。
2.應(yīng)用外部存儲(chǔ)路徑
Android系統(tǒng)也支持在外部存儲(chǔ)設(shè)備上創(chuàng)建、讀寫(xiě)應(yīng)用程序的數(shù)據(jù)庫(kù)文件。這對(duì)于需要存儲(chǔ)較大量數(shù)據(jù)的應(yīng)用程序來(lái)說(shuō)是十分重要的,因?yàn)閮?nèi)部存儲(chǔ)空間有限。外部的數(shù)據(jù)庫(kù)將被存儲(chǔ)在 /mnt/sdcard/Android/data//database/ 目錄下。其中,是應(yīng)用程序的包名。同時(shí)需要在AndroidManifest.xml文件中添加如下權(quán)限:
二、SQLite數(shù)據(jù)庫(kù)API
安卓系統(tǒng)中,sqlite數(shù)據(jù)庫(kù)API包含4個(gè)類:SQLiteOpenHelper、SQLiteDatabase、Cursor和DatabaseUtils。
1.SQLiteOpenHelper
SQLiteOpenHelper可以協(xié)助開(kāi)發(fā)人員實(shí)現(xiàn)數(shù)據(jù)庫(kù)的創(chuàng)建和升級(jí)。其主要包含了onCreate()、onUpgrade()和getReadableDatabase()、getWritableDatabase()等方法。其中,onCreate()用于在數(shù)據(jù)庫(kù)被創(chuàng)建時(shí)執(zhí)行初始化操作;onUpgrade()用于在數(shù)據(jù)庫(kù)版本更新時(shí)執(zhí)行更新操作;getReadableDatabase()和getWritableDatabase()用于獲取可讀可寫(xiě)的數(shù)據(jù)庫(kù)實(shí)例。
2.SQLiteDatabase
SQLiteDatabase類是SQLiteOpenHelper的子類,其可以理解為對(duì)SQLite數(shù)據(jù)庫(kù)的封裝。我們可以使用SQLiteDatabase類對(duì)數(shù)據(jù)庫(kù)進(jìn)行創(chuàng)建、升級(jí)、查詢、更新和刪除操作。
3.Cursor
Cursor是查詢結(jié)果的封裝。使用Cursor可以方便地獲取查詢結(jié)果中的每一個(gè)條目。
4.DatabaseUtils
DatabaseUtils類是一個(gè)工具類,主要提供了一些常用的數(shù)據(jù)庫(kù)操作。比如將對(duì)象傳遞給ContentValues,將Cursor轉(zhuǎn)化為L(zhǎng)ist等操作。
舉例說(shuō)明,以下是一個(gè)創(chuàng)建數(shù)據(jù)庫(kù)表的示例。
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)問(wèn)題拓展閱讀:
- Android 請(qǐng)問(wèn)如何更改android 數(shù)據(jù)庫(kù)的存儲(chǔ)路徑
- 在Android中使用SQLite,用getWritableDatabase()方法創(chuàng)建后,數(shù)據(jù)庫(kù)文件在哪兒?
Android 請(qǐng)問(wèn)如何更改android 數(shù)據(jù)庫(kù)的存儲(chǔ)路徑
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)來(lái)管攜吵理,應(yīng)該自己設(shè)定一個(gè)更大容量,當(dāng)超出這個(gè)值時(shí)自己刪除。
Context.getFilesDir(),Context.openFileOutput(String, int),Context.getFileStreamPath(String),Context.getDir(String, int)
/data/data/files
Android支持在SD卡上的應(yīng)用私有目錄,核隱慶在改握Froyo版本后,通過(guò)getExternalFilesDir()可以獲得具體路徑。該路徑依賴與應(yīng)用的包名,如果你包為hello.file那么SD開(kāi)上的應(yīng)用私有目錄為\mnt\sdcard\Android\data\hello.file\files\.
在使用SD卡目錄時(shí),需注意SD卡是否掛載,可通過(guò)Environment.getExternalStorageState()方法進(jìn)行判斷,如果返回值為Envirnment.MEDIA_MOUNTED表示SD卡處于掛載狀態(tài),可以放心使用。
getExternalCacheDir()和getCacheDir()比較
共同點(diǎn):
files will be deleted when the application is uninstalled
不同點(diǎn):
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ù)庫(kù)文件在哪兒?
數(shù)據(jù)庫(kù)存放在 /data/data/PACKAGE_NAME/databases 目錄明褲下
你當(dāng)然可以指定數(shù)據(jù)庫(kù)名字,可以將db文件打包在工程里。
private SQLiteDatabase openDatabase() {
try {
// 獲得dictionary.db文件的絕對(duì)路徑
String databaseFilename = DATABASE_PATH + “/” + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
// 如果/sdcard/dictionary目錄中存在,創(chuàng)建這個(gè)目錄
if (!dir.exists())
dir.mkdir();
// 如果在/sdcard/dictionary目錄中不存在
// dictionary.db文件,則從res\激巖簡(jiǎn)raw目錄中復(fù)制這個(gè)文件到
// SD卡的目錄(/sdcard/棗畝dictionary)
if (!(new File(databaseFilename)).exists()) {
// 獲得封裝dictionary.db文件的InputStream對(duì)象
InputStream is = getResources().openRawResource(
R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte buffer = new byte;
int count = 0;
// 開(kāi)始復(fù)制dictionary.db文件
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
// 打開(kāi)/sdcard/dictionary目錄中的dictionary.db文件
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
} catch (Exception e) {
}
return null;
}
android數(shù)據(jù)庫(kù)存放地址的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于android數(shù)據(jù)庫(kù)存放地址,Android數(shù)據(jù)庫(kù):存放位置詳解,Android 請(qǐng)問(wèn)如何更改android 數(shù)據(jù)庫(kù)的存儲(chǔ)路徑,在Android中使用SQLite,用getWritableDatabase()方法創(chuàng)建后,數(shù)據(jù)庫(kù)文件在哪兒?的信息別忘了在本站進(jìn)行查找喔。
成都服務(wù)器托管選創(chuàng)新互聯(lián),先上架開(kāi)通再付費(fèi)。
創(chuàng)新互聯(lián)(www.cdcxhl.com)專業(yè)-網(wǎng)站建設(shè),軟件開(kāi)發(fā)老牌服務(wù)商!微信小程序開(kāi)發(fā),APP開(kāi)發(fā),網(wǎng)站制作,網(wǎng)站營(yíng)銷推廣服務(wù)眾多企業(yè)。電話:028-86922220
分享名稱:Android數(shù)據(jù)庫(kù):存放位置詳解 (android數(shù)據(jù)庫(kù)存放地址)
文章鏈接:http://m.fisionsoft.com.cn/article/cohdjoh.html


咨詢
建站咨詢
