如何将SQLite数据库(dictionary.db文件)与apk文件一起发布
2016-09-02 15:25
393 查看
可以将dictionary.db文件复制到Eclipse Android工程中的res\raw目录中,如图1所示。所有在res\raw目录中的文件不会被压缩,这样可以直接提取该目录中的文件。
使用openDatabase方法来打开数据库文件,如果该文件不存在,系统会自动创建/sdcard/dictionary目录,并将res\raw目录中的 dictionary.db文件复制到/sdcard/dictionary目录中。openDatabase方法的实现代码如下:
代码
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[8192];
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;
}
在openDatabase方法中使用了几个常量,这些常量是在程序的主类(Main)中定义的,代码如下:
代码
public class Main extends Activity implements OnClickListener, TextWatcher
{
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/dictionary";
private final String DATABASE_FILENAME = "dictionary.db";
}
使用openDatabase方法来打开数据库文件,如果该文件不存在,系统会自动创建/sdcard/dictionary目录,并将res\raw目录中的 dictionary.db文件复制到/sdcard/dictionary目录中。openDatabase方法的实现代码如下:
代码
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[8192];
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;
}
在openDatabase方法中使用了几个常量,这些常量是在程序的主类(Main)中定义的,代码如下:
代码
public class Main extends Activity implements OnClickListener, TextWatcher
{
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/dictionary";
private final String DATABASE_FILENAME = "dictionary.db";
}
相关文章推荐
- mysql_config_editor的用法
- MySQL跳过主从错误工具(mysqlha_skiperror.sh)
- Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号
- redis 简单的缓存
- MongoDB系列一:MongoDB安装、启动关闭服务、客户端连接
- 常用SQL语句
- oracle sql编程笔记
- oracle创办表时候指定表空间
- 如何给oracle账户解锁
- 高性能数据库连接池的内幕
- CentOS下为php安装memcached扩展模块
- 如何用easyUI做Oracle的分页
- MySQL中创建用户
- linux开机启动mongodb
- MariaDBm 基于SSL的主从复制配置
- MYSQL 安装教程二
- redis和memecache的区别
- Redis常用命令
- Solaris10上安装Redis3.0.6
- mysql 安装于使用 测试 可视化工具