- 浏览: 3964963 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
hinuliba:
...
字体背景颜色的高度修改 -
KANGOD:
最后的 -createDialog() 私有方法是怎么回事,没 ...
简单的实现listView中item多个控件以及点击事件 -
sswangqiao:
呵呵,呵呵
onActivityResult传值的使用 -
yumeiqiao:
感觉你所的不清楚 lstView.setOnTouchLi ...
listview中viewflipper的问题 -
lizhou828:
果然是大神啊!!!
Animation动画效果的实现
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Most all of the Android examples and tutorials out there assume you want to create and populate your database at runtime and not to use and access an independent, preloaded database with your Android application.
The method I'm going to show you takes your own SQLite database file from the "assets" folder and copies into the system database path of your application so the SQLiteDatabase API can open and access it normally.
1. Preparing the SQLite database file.
Assuming you already have your sqlite database created, we need to do some modifications to it.
If you don't have a sqlite manager I recommend you to download the opensource SQLite Database Browser available for Win/Linux/Mac.
Open your database and add a new table called "android_metadata", you can execute the following SQL statement to do it:
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
Now insert a single row with the text 'en_US' in the "android_metadata" table:
INSERT INTO "android_metadata" VALUES ('en_US')
Then, it is necessary to rename the primary id field of your tables to "_id" so Android will know where to bind the id field of your tables.
You can easily do this with SQLite Database Browser by pressing the edit table button , then selecting the table you want to edit and finally selecting the field you want to rename.
After renaming the id field of all your data tables to "_id" and adding the "android_metadata" table, your database it's ready to be used in your Android application.

Modified database
Note: in this image we see the tables "Categories" and "Content" with the id field renamed to "_id" and the just added table "android_metadata".
2. Copying, opening and accessing your database in your Android application.
Now just put your database file in the "assets" folder of your project and create a Database Helper class by extending the SQLiteOpenHelper class from the "android.database.sqlite" package.
Make your DataBaseHelper class look like this:
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. }
That's it.
Now you can create a new instance of this DataBaseHelper class and call the createDataBase() and openDataBase() methods. Remember to change the "YOUR_PACKAGE" to your application package namespace (i.e: com.examplename.myapp) in the DB_PATH string.
... 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; } ...
Enjoyed this post? ReignDesign is a great team of tech-savvy developers providing RIA and mobile services. For more articles like this, subscribe to our blog feed.
发表评论
-
URI 转path
2019-06-26 10:41 1374转自知乎Matisse package com.zhihu ... -
权限申请
2017-09-22 13:25 1304public class PermissionActivit ... -
onPreviewFrame 相机输出格式转换yuv420p保存成图片
2015-11-25 15:59 7635在最近项目中,因为特殊需要,底层相机往外输出了i420 也 ... -
new Android's Runtime Permission
2015-11-03 21:05 1273targetSdkVersion 23 开始 使用运行时权 ... -
自定义listview 边缘效果
2015-02-28 10:58 1779static void ChangeEdgeEffect( ... -
发射打开wifi
2015-01-07 10:25 1469WifiManager wifiManager = (Wif ... -
RecyclerView
2014-11-05 13:08 1307http://www.grokkingandroid.com ... -
获取点击区域
2014-04-28 09:39 1606@Override public void getHitR ... -
speex 和libogg 编译
2014-04-03 16:17 6428下载: http://www.speex.org/down ... -
rsync 同步
2014-03-28 17:06 1876两台android 设备 进行rsy ... -
流转字符串
2014-03-11 09:49 1580public static String convertSt ... -
java simplexml 序列化
2014-03-06 13:22 6016<?xml version="1.0&quo ... -
获取其他程序的特定资源
2014-03-05 09:33 1728try { PackageManager man ... -
检测来电属于哪个sim卡
2014-02-07 10:41 1768public class IncomingCallInter ... -
使用 NDK r9 编译ffmpeg
2014-01-16 13:32 168881. 环境 ubuntu 我的是13.10 ndk r9 ... -
android h264含so
2014-01-13 11:24 1580http://download.csdn.net/downlo ... -
xml转义字符
2013-12-18 09:29 1615" " ' & ... -
字体背景颜色的高度修改
2013-12-11 10:31 4279当使用android:lineSpacingExtra= ... -
屏保的实现
2013-12-07 10:27 2876最近需要做一个屏保,开始以为很简单,因为系统本身就带了屏保功 ... -
PreferenceActivity下嵌套PreferenceScreen在其它布局中
2013-11-21 16:32 9214今天在修改系统代码的时候,系统代码用了PreferenceA ...
相关推荐
SQLite Database 是一种轻量级的、开源的、自包含的桌面数据库系统,它无需服务器进程,可以直接在用户的应用程序中使用。这个工具以其高效、可靠和易于集成的特点,被广泛应用于各种小型到中型的数据存储需求,尤其...
SQLite数据库编辑工具SQLiteDatabaseBrowser是一款强大的开源工具,专为管理和编辑SQLite数据库而设计。SQLite是一种轻量级的、自包含的、无服务器的、事务性的SQL数据库引擎,广泛应用于移动设备、嵌入式系统以及...
SQLite Database Browser是一款直观的图形用户界面(GUI)工具,专门设计用于管理和操作SQLite数据库。SQLite是一种轻量级、自包含、开源的关系型数据库管理系统,广泛应用于移动设备、嵌入式系统以及桌面应用程序中...
SQLite Database Browser 是一款专为Mac OS设计的直观易用的数据库管理工具,它专注于处理SQLite数据库文件。SQLite是一种轻量级、自包含的数据库引擎,广泛应用于移动设备、嵌入式系统以及桌面应用中,因为它不需要...
本文将深入探讨SQLite在Android中的使用,包括基本概念、数据库操作以及实际应用示例。 ### 1. SQLite基本概念 - **数据库**: 数据库是存储和组织数据的结构化系统,SQLite在Android中表现为一个数据库文件,扩展...
从标题《SQLite Database System Design and Implementation》以及提供的部分内容来看,本书的核心知识点包括: 1. 关系型数据库管理系统(RDBMS)的基本概念:本书在开头部分回顾了关系型数据库的一些基础概念,...
2. **连接数据库**:使用JDBC URL(如`jdbc:sqlite:/path/to/database`)建立连接。 3. **执行SQL语句**:通过`Statement`或`PreparedStatement`对象执行SQL命令,如`CREATE TABLE`、`INSERT INTO`、`SELECT`等。 4....
SQLite Database Browser是一款功能强大的、免费开源的SQLite数据库管理工具,专为Windows用户设计。它允许用户在无需深入了解SQL语法的情况下创建、编辑和管理SQLite数据库。SQLite本身是一个轻量级的、自包含的、...
在使用SQLite Database Browser时,首先需要了解SQLite的基本概念,如数据库(Database)、表(Table)、列(Column)、记录(Row)以及SQL语言的基础知识。SQL(Structured Query Language)是用于管理关系数据库的...
在Qt中使用SQLite3数据库进行增删改查操作,首先需要在Qt项目中包含相应的SQLite模块。从Qt 5开始,SQLite数据库支持被直接集成在Qt SQL模块中,无需额外安装SQLite数据库软件。 以下是如何在Qt项目中使用SQLite3...
`SQLiteDatabaseBrowser`是一款专为SQLite数据库设计的轻量级图形用户界面(GUI)客户端工具。这个工具的主要目的是为了方便非技术用户以及开发者能够轻松地创建、查看、修改和管理SQLite数据库。SQLite本身是一种...
在Android应用程序中使用自己的SQLite数据库是一项常见的需求,特别是在已有数据集或者希望预加载特定数据时。这个过程包括准备SQLite数据库文件、复制到应用的数据库路径以及在应用中打开和访问数据库。以下是一份...
在Android开发中,SQLite是一个非常...以上就是关于Android中使用SQLite数据库的基本知识,包括创建数据库、指定数据库位置以及进行各种数据库操作的方法。通过这些基础,开发者可以构建出满足需求的数据存储解决方案。
使用SQLite Database Browser,开发者和数据库管理员可以方便地进行数据库设计、调试和维护工作,而无需深入了解SQLite的底层实现。它简化了SQLite数据库的管理和开发流程,尤其适合小型项目或个人开发者的使用。...
这通常通过创建一个新的SQLite3文件,然后使用`INSERT INTO`或`ATTACH DATABASE`语句将内存数据库的内容转移到这个新文件。 5. **使用SQLite3 Simple Delphi包装类** 这种包装类简化了与SQLite3的交互过程,提供了...
SQLite Database Browser 2.0 是一个专为mac用户设计的SQLite数据库管理工具,它提供了直观且易用的界面,使得数据库的浏览、管理和操作变得轻松。对于那些在Android开发中处理SQLite数据库的开发者来说,这个工具...
以下是对“wince操作数据库Sqlite”这一主题的详细说明。 **SQLite介绍** SQLite是一个开源的数据库系统,它的设计目标是小型、快速和可靠。SQLite数据库文件是一个普通的文件,可以随应用程序一起复制,不需要专门...
Android数据库SQLite手动建库的代码页,适合小白学习分析Android代码。即便是刚刚接触的Android的学生,看里面的两行注释也能明白。 程序运行效果,在File Explore里面的Date/Date/<packageName>/database下会看到...
- 创建数据库:`sqlite3_open("my_database.db", &db)`打开或创建名为"my_database.db"的数据库,`db`是一个指向SQLite3连接的指针。 - 执行SQL:`sqlite3_exec(db, "CREATE TABLE my_table (id INTEGER PRIMARY ...
SQLite Database Browser 2.0 b1.exe是SQLiteBrowser的可执行程序文件。 总的来说,SQLiteBrowser是一款强大的SQLite数据库管理和查看工具,它通过友好的图形界面简化了数据库操作,对开发者和非开发人员都极具价值...