很多时候在应用安装初始化时,需要创建本地数据库,同时为数据库添加数据,之后再从数据库中读取数据。

这里有2个思路

1.先在本地创建一个能支持android使用的sqlite数据库文件,启动时,用现成的sqlite的二进制文件进行直接copy到Android系统的数据库路径

2.可以考虑在第一次启动时,执行数据库初始化的sql文件.

1.在本地准备android能使用的sqlite数据库文件

使用sqlite数据库管理工具,看个人爱好(SQLite Database Browser ,Navicat Premium,)

打开数据库,创建"android_metadata"数据表

Sql代码  
  1. CREATE TABLE "android_metadata" ("_id"  INTEGER PRIMARY KEY AUTOINCREMENT,"locale" TEXT DEFAULT 'en_US');//创建表
  2. INSERT INTO "android_metadata" VALUES (1,'en_US');//插入值

创建其他应用需要的表..此处省略.

2.复制文件到应用中.

把第一步创建的数据库文件复制到应用中的assets文件夹, asserts文件夹的路径如下:

然后创建DateBaseHelper extends SQLiteOpenHelper的类文件.

代码如下:

Java代码  
  1. public class DataBaseHelper extends SQLiteOpenHelper {
  2. //The Android's default system path of your application database.
  3. private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
  4. private static String DB_NAME = "myDBName";
  5. private SQLiteDatabase myDataBase;
  6. private final Context myContext;
  7. /**
  8. * Constructor
  9. * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
  10. * @param context
  11. */
  12. public DataBaseHelper(Context context) {
  13. super(context, DB_NAME, null, 1);
  14. this.myContext = context;
  15. }
  16. /**
  17. * Creates a empty database on the system and rewrites it with your own database.
  18. * */
  19. public void createDataBase()throws IOException {
  20. boolean dbExist = checkDataBase();
  21. if (dbExist) {
  22. //do nothing - database already exist
  23. } else {
  24. //By calling this method and empty database will be created into the default system path
  25. //of your application so we are gonna be able to overwrite that database with our database.
  26. this.getReadableDatabase();
  27. try {
  28. copyDataBase();
  29. } catch (IOException e) {
  30. throw new Error("Error copying database");
  31. }
  32. }
  33. }
  34. /**
  35. * Check if the database already exist to avoid re-copying the file each time you open the application.
  36. * @return true if it exists, false if it doesn't
  37. */
  38. private boolean checkDataBase() {
  39. SQLiteDatabase checkDB = null;
  40. try {
  41. String myPath = DB_PATH + DB_NAME;
  42. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  43. } catch (SQLiteException e) {
  44. //database does't exist yet.
  45. }
  46. if (checkDB != null) {
  47. checkDB.close();
  48. }
  49. return checkDB != null ? true : false;
  50. }
  51. /**
  52. * Copies your database from your local assets-folder to the just created empty database in the
  53. * system folder, from where it can be accessed and handled.
  54. * This is done by transfering bytestream.
  55. * */
  56. private void copyDataBase()throws IOException {
  57. //Open your local db as the input stream
  58. InputStream myInput = myContext.getAssets().open(DB_NAME);
  59. // Path to the just created empty db
  60. String outFileName = DB_PATH + DB_NAME;
  61. //Open the empty db as the output stream
  62. OutputStream myOutput = new FileOutputStream(outFileName);
  63. //transfer bytes from the inputfile to the outputfile
  64. byte[]buffer = new byte[1024];
  65. int length;
  66. while ((length = myInput.read(buffer)) > 0) {
  67. myOutput.write(buffer, 0, length);
  68. }
  69. //Close the streams
  70. myOutput.flush();
  71. myOutput.close();
  72. myInput.close();
  73. }
  74. public void openDataBase()throws SQLException {
  75. //Open the database
  76. String myPath = DB_PATH + DB_NAME;
  77. myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  78. }
  79. @Override
  80. public synchronized void close() {
  81. if (myDataBase != null)
  82. myDataBase.close();
  83. super.close();
  84. }
  85. @Override
  86. public void onCreate(SQLiteDatabase db) {}
  87. @Override
  88. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
  89. // Add your public helper methods to access and get content from the database.
  90. // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
  91. // to you to create adapters for your views.
  92. }

3.现在我们可以创建DateBaseHelper的实现操作了.

createDataBase() //创建

openDataBase()//打开只读数据库

记得要更改"YOUR_PACKAGE"为你的应用的包名

如:com.examplename.myapp

大概代码如下:

Java代码  
  1. ...
  2. DataBaseHelper myDbHelper = new DataBaseHelper();
  3. myDbHelper = new DataBaseHelper(this);
  4. try {
  5. myDbHelper.createDataBase();
  6. } catch (IOException ioe) {
  7. throw new Error("Unable to create database");
  8. }
  9. try {
  10. myDbHelper.openDataBase();
  11. } catch (SQLException sqle) {
  12. throw sqle;
  13. }
  14. ...  
    来源: http://zhangfan822.iteye.com/blog/1883829

转载于:https://www.cnblogs.com/strinkbug/p/6858702.html

android:Android中用文件初始化sqlite数据库(zz)相关推荐

  1. Android中用文件初始化sqlite 数据库(二)

    博 androidsqlite启动时数据库初始化  方法1已经讲述了一种初始化数据库的方法 它的数据库初始化不是用sql语句,而是用一个现成的sqlite的二进制文件进行直接copy到Android系 ...

  2. 在 Android 应用程序中使用 SQLite 数据库以及怎么用

    part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...

  3. android模拟器的数据存放,Android模拟器在哪里存储SQLite数据库?

    Android模拟器在哪里存储SQLite数据库? 我正在开发一个将数据存储在SQLite数据库中的Android应用程序. 我的问题是,当您使用模拟器时,此数据库文件存储在文件系统中的哪个位置? 我 ...

  4. 在Android中查看和管理sqlite数据库

    在Android中查看和管理sqlite数据库 在Android中可以使用Eclipse插件DDMS来查看,也可以使用Android工具包中的adb工具来查看.android项目中的sqlite数据库 ...

  5. android打开sqlite数据库,Android:打开和关闭SQLite数据库

    我正在开发和android应用程序,我经常使用它访问本地数据库.这个数据库可以从不同的therads访问,所以我对数据库有一个协调问题.我使用以下open()和close()方法.Android:打开 ...

  6. MS Access 教程之如何将 MDB 文件转换为 SQLite 数据库

    那么如何将 MDB 文件转换为 SQLite 数据库呢?归根结底,最简单的方法是创建一个 MDB 数据库,即半自动转换其他 MDB 文件.我想要一个类似这样的面具: 但在此之前,我必须配置 PC.我从 ...

  7. android sqlite 添加多个表,Android的 - 导入多个.CSV文件在SQLite数据库的多个表(Android - Impo...

    我在我的Android设备有多个.CSV文件的文件夹. 我想所有的人都导入到我的SQLite数据库,但每个文件必须是不同的表. 所有的.csv文件中简单. 他们只有一个列. 例: FILE.CSV 1 ...

  8. Android内部自带的SQLite数据库操作dos命令

    1:什么叫做SQLite数据库 Android系统内核是Linux系统,Android系统很特殊,他自带了一个SQLite数据库,轻量型的一款嵌入式的数据库 它占用资源非常的低,在嵌入式设备中,可能只 ...

  9. android studio SQLScout插件查看sqlite数据库

    SQLScout (SQLite Support) SQLScout (SQLite Support) 是android studio集成开发工具中查看SQLite数据库的插件. 1.安装 file- ...

最新文章

  1. 世界上最百变的人不是女友,竟然是......
  2. centos7升级php版本
  3. Mark Links@2012/8/25
  4. OVS openflow(二十四)
  5. java属于面相_[Java教程]面相对象
  6. Android Studio 修改项目包名(任意级)
  7. linux中逻辑块大小为,Linux 文件系统相关的基本概念
  8. python 爬取直播_python---爬取某鱼直播
  9. 57、RapidJson存储Base64数据和空间释放
  10. linux有没有crt软件,linux类似windows下secureCRT的软件
  11. STRUTS 2 教程
  12. oracle 修改pkg命令,oracle简单PKG(包)编写
  13. 图形驱动程序和显卡驱动什么区别_更新电脑显卡驱动有什么作用 更新电脑显卡驱动操作介绍【详解】...
  14. IT行业就业前景如何
  15. IPD的决策评审DCP(1):概念、战略性
  16. Facebook POP 动画框架 进阶指南
  17. PostMan测试接口,出现415报错,Unsupported Media Type
  18. 之杰的机器学习笔记:1.机器学习概述
  19. 如何解决Unsupported Architecture. Your executable contains unsupported architecture '[x86_64, i386]
  20. SAXReader的主要用法(XML)

热门文章

  1. JavaScript————FormData实现多文件上传
  2. esp mounter pro_对比 | 以大欺小?剑指宋Pro和哈弗H6,欧尚X7的黑马潜质从何而来?...
  3. 更新无限无线连接更新服务器,02-H3C WBC560多业务无线控制器软件升级操作指导...
  4. pdf从结构新建书签_强力推荐一款PDF神器
  5. java swing linux_Linux下关于解决JavaSwing中文乱码的情况
  6. nokia 计算机手机,NOKIA手机与电脑的数据线连接
  7. linux+系统优化基础,Linux入门基础(三):Linux用户及权限基础
  8. Java 多个异常处理
  9. linux 常用头文件,(常用头文件详解.doc
  10. linux mysql 8安装教程,MySQL8系列安装与配置教程(Linux环境)