android:Android中用文件初始化sqlite数据库(zz)
这里有2个思路
1.先在本地创建一个能支持android使用的sqlite数据库文件,启动时,用现成的sqlite的二进制文件进行直接copy到Android系统的数据库路径
2.可以考虑在第一次启动时,执行数据库初始化的sql文件.
1.在本地准备android能使用的sqlite数据库文件
使用sqlite数据库管理工具,看个人爱好(SQLite Database Browser ,Navicat Premium,)
打开数据库,创建"android_metadata"数据表
- CREATE TABLE "android_metadata" ("_id" INTEGER PRIMARY KEY AUTOINCREMENT,"locale" TEXT DEFAULT 'en_US');//创建表
- INSERT INTO "android_metadata" VALUES (1,'en_US');//插入值
创建其他应用需要的表..此处省略.
2.复制文件到应用中.
把第一步创建的数据库文件复制到应用中的assets文件夹, asserts文件夹的路径如下:
然后创建DateBaseHelper extends SQLiteOpenHelper的类文件.
代码如下:
- public class DataBaseHelper extends SQLiteOpenHelper {
- //The Android's default system path of your application database.
- private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
- private static String DB_NAME = "myDBName";
- private SQLiteDatabase myDataBase;
- private final Context myContext;
- /**
- * Constructor
- * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
- * @param context
- */
- public DataBaseHelper(Context context) {
- super(context, DB_NAME, null, 1);
- this.myContext = context;
- }
- /**
- * Creates a empty database on the system and rewrites it with your own database.
- * */
- public void createDataBase()throws IOException {
- boolean dbExist = checkDataBase();
- if (dbExist) {
- //do nothing - database already exist
- } else {
- //By calling this method and empty database will be created into the default system path
- //of your application so we are gonna be able to overwrite that database with our database.
- this.getReadableDatabase();
- try {
- copyDataBase();
- } catch (IOException e) {
- throw new Error("Error copying database");
- }
- }
- }
- /**
- * Check if the database already exist to avoid re-copying the file each time you open the application.
- * @return true if it exists, false if it doesn't
- */
- private boolean checkDataBase() {
- SQLiteDatabase checkDB = null;
- try {
- String myPath = DB_PATH + DB_NAME;
- checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
- } catch (SQLiteException e) {
- //database does't exist yet.
- }
- if (checkDB != null) {
- checkDB.close();
- }
- return checkDB != null ? true : false;
- }
- /**
- * Copies your database from your local assets-folder to the just created empty database in the
- * system folder, from where it can be accessed and handled.
- * This is done by transfering bytestream.
- * */
- private void copyDataBase()throws IOException {
- //Open your local db as the input stream
- InputStream myInput = myContext.getAssets().open(DB_NAME);
- // Path to the just created empty db
- String outFileName = DB_PATH + DB_NAME;
- //Open the empty db as the output stream
- OutputStream myOutput = new FileOutputStream(outFileName);
- //transfer bytes from the inputfile to the outputfile
- byte[]buffer = new byte[1024];
- int length;
- while ((length = myInput.read(buffer)) > 0) {
- myOutput.write(buffer, 0, length);
- }
- //Close the streams
- myOutput.flush();
- myOutput.close();
- myInput.close();
- }
- public void openDataBase()throws SQLException {
- //Open the database
- String myPath = DB_PATH + DB_NAME;
- myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
- }
- @Override
- public synchronized void close() {
- if (myDataBase != null)
- myDataBase.close();
- super.close();
- }
- @Override
- public void onCreate(SQLiteDatabase db) {}
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
- // Add your public helper methods to access and get content from the database.
- // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
- // to you to create adapters for your views.
- }
3.现在我们可以创建DateBaseHelper的实现操作了.
createDataBase() //创建
openDataBase()//打开只读数据库
记得要更改"YOUR_PACKAGE"为你的应用的包名
如:com.examplename.myapp
大概代码如下:
- ...
- DataBaseHelper myDbHelper = new DataBaseHelper();
- myDbHelper = new DataBaseHelper(this);
- try {
- myDbHelper.createDataBase();
- } catch (IOException ioe) {
- throw new Error("Unable to create database");
- }
- try {
- myDbHelper.openDataBase();
- } catch (SQLException sqle) {
- throw sqle;
- }
- ...
来源: http://zhangfan822.iteye.com/blog/1883829
转载于:https://www.cnblogs.com/strinkbug/p/6858702.html
android:Android中用文件初始化sqlite数据库(zz)相关推荐
- Android中用文件初始化sqlite 数据库(二)
博 androidsqlite启动时数据库初始化 方法1已经讲述了一种初始化数据库的方法 它的数据库初始化不是用sql语句,而是用一个现成的sqlite的二进制文件进行直接copy到Android系 ...
- 在 Android 应用程序中使用 SQLite 数据库以及怎么用
part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...
- android模拟器的数据存放,Android模拟器在哪里存储SQLite数据库?
Android模拟器在哪里存储SQLite数据库? 我正在开发一个将数据存储在SQLite数据库中的Android应用程序. 我的问题是,当您使用模拟器时,此数据库文件存储在文件系统中的哪个位置? 我 ...
- 在Android中查看和管理sqlite数据库
在Android中查看和管理sqlite数据库 在Android中可以使用Eclipse插件DDMS来查看,也可以使用Android工具包中的adb工具来查看.android项目中的sqlite数据库 ...
- android打开sqlite数据库,Android:打开和关闭SQLite数据库
我正在开发和android应用程序,我经常使用它访问本地数据库.这个数据库可以从不同的therads访问,所以我对数据库有一个协调问题.我使用以下open()和close()方法.Android:打开 ...
- MS Access 教程之如何将 MDB 文件转换为 SQLite 数据库
那么如何将 MDB 文件转换为 SQLite 数据库呢?归根结底,最简单的方法是创建一个 MDB 数据库,即半自动转换其他 MDB 文件.我想要一个类似这样的面具: 但在此之前,我必须配置 PC.我从 ...
- android sqlite 添加多个表,Android的 - 导入多个.CSV文件在SQLite数据库的多个表(Android - Impo...
我在我的Android设备有多个.CSV文件的文件夹. 我想所有的人都导入到我的SQLite数据库,但每个文件必须是不同的表. 所有的.csv文件中简单. 他们只有一个列. 例: FILE.CSV 1 ...
- Android内部自带的SQLite数据库操作dos命令
1:什么叫做SQLite数据库 Android系统内核是Linux系统,Android系统很特殊,他自带了一个SQLite数据库,轻量型的一款嵌入式的数据库 它占用资源非常的低,在嵌入式设备中,可能只 ...
- android studio SQLScout插件查看sqlite数据库
SQLScout (SQLite Support) SQLScout (SQLite Support) 是android studio集成开发工具中查看SQLite数据库的插件. 1.安装 file- ...
最新文章
- 世界上最百变的人不是女友,竟然是......
- centos7升级php版本
- Mark Links@2012/8/25
- OVS openflow(二十四)
- java属于面相_[Java教程]面相对象
- Android Studio 修改项目包名(任意级)
- linux中逻辑块大小为,Linux 文件系统相关的基本概念
- python 爬取直播_python---爬取某鱼直播
- 57、RapidJson存储Base64数据和空间释放
- linux有没有crt软件,linux类似windows下secureCRT的软件
- STRUTS 2 教程
- oracle 修改pkg命令,oracle简单PKG(包)编写
- 图形驱动程序和显卡驱动什么区别_更新电脑显卡驱动有什么作用 更新电脑显卡驱动操作介绍【详解】...
- IT行业就业前景如何
- IPD的决策评审DCP(1):概念、战略性
- Facebook POP 动画框架 进阶指南
- PostMan测试接口,出现415报错,Unsupported Media Type
- 之杰的机器学习笔记:1.机器学习概述
- 如何解决Unsupported Architecture. Your executable contains unsupported architecture '[x86_64, i386]
- SAXReader的主要用法(XML)
热门文章
- JavaScript————FormData实现多文件上传
- esp mounter pro_对比 | 以大欺小?剑指宋Pro和哈弗H6,欧尚X7的黑马潜质从何而来?...
- 更新无限无线连接更新服务器,02-H3C WBC560多业务无线控制器软件升级操作指导...
- pdf从结构新建书签_强力推荐一款PDF神器
- java swing linux_Linux下关于解决JavaSwing中文乱码的情况
- nokia 计算机手机,NOKIA手机与电脑的数据线连接
- linux+系统优化基础,Linux入门基础(三):Linux用户及权限基础
- Java 多个异常处理
- linux 常用头文件,(常用头文件详解.doc
- linux mysql 8安装教程,MySQL8系列安装与配置教程(Linux环境)