Android数据库:存放位置详解 (android数据库存放地址)

Android系统的数据库是应用程序中常用的数据存储方式,而Android系统中的数据库文件可能存在不同的存放位置,本文将为读者介绍Android数据库存放位置的详细情况。

一、内部存储

1.应用内数据库路径

在Android系统中,每一个应用程序都会有其专属的内部存储空间,并且每一个应用程序可以有自己的内部数据库。在该应用程序中创建的数据库文件将被默认存储在/data/data//databases/ 目录下。其中,是应用程序的包名。

2.应用外部存储路径

Android系统也支持在外部存储设备上创建、读写应用程序的数据库文件。这对于需要存储较大量数据的应用程序来说是十分重要的,因为内部存储空间有限。外部的数据库将被存储在 /mnt/sdcard/Android/data//database/ 目录下。其中,是应用程序的包名。同时需要在AndroidManifest.xml文件中添加如下权限:

二、SQLite数据库API

安卓系统中,sqlite数据库API包含4个类:SQLiteOpenHelper、SQLiteDatabase、Cursor和DatabaseUtils。

1.SQLiteOpenHelper

SQLiteOpenHelper可以协助开发人员实现数据库的创建和升级。其主要包含了onCreate()、onUpgrade()和getReadableDatabase()、getWritableDatabase()等方法。其中,onCreate()用于在数据库被创建时执行初始化操作;onUpgrade()用于在数据库版本更新时执行更新操作;getReadableDatabase()和getWritableDatabase()用于获取可读可写的数据库实例。

2.SQLiteDatabase

SQLiteDatabase类是SQLiteOpenHelper的子类,其可以理解为对SQLite数据库的封装。我们可以使用SQLiteDatabase类对数据库进行创建、升级、查询、更新和删除操作。

3.Cursor

Cursor是查询结果的封装。使用Cursor可以方便地获取查询结果中的每一个条目。

4.DatabaseUtils

DatabaseUtils类是一个工具类,主要提供了一些常用的数据库操作。比如将对象传递给ContentValues,将Cursor转化为List等操作。

举例说明,以下是一个创建数据库表的示例。

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);

}

}

}

三、

相关问题拓展阅读:

Android 请问如何更改android 数据库的存储路径

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.

但是,更好不要依赖系统来管携吵理,应该自己设定一个更大容量,当超出这个值时自己删除。

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卡处于挂载状态,可以放心使用。

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()方法创建后,数据库文件在哪儿?

数据库存放在 /data/data/PACKAGE_NAME/databases 目录明裤下

你当然可以指定数据库名字,可以将db文件打包在工程里。

private SQLiteDatabase openDatabase() {

try {

// 获得dictionary.db文件的绝对路径

String databaseFilename = DATABASE_PATH + “/” + DATABASE_FILENAME;

File dir = new File(DATABASE_PATH);

// 如果/sdcard/dictionary目录中存在,创建这个目录

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数据库存放地址的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于android数据库存放地址,Android数据库:存放位置详解,Android 请问如何更改android 数据库的存储路径,在Android中使用SQLite,用getWritableDatabase()方法创建后,数据库文件在哪儿?的信息别忘了在本站进行查找喔。


数据运维技术 » Android数据库:存放位置详解 (android数据库存放地址)