- 浏览: 159303 次
- 性别:
- 来自: 大连
最新评论
-
撒旦的堕落:
引用引用引用引用引用引用引用引用引用引用引用引用引用引用引用引 ...
★Struts2中的Ajax实现 <s:a> Ⅱ -
撒旦的堕落:
引用引用引用引用引用引用引用引用引用引用引用引用引用引用引用引 ...
★Struts2中的Ajax实现 <s:a> Ⅱ -
crazyj2ee:
[img][/img]
flex 3 从页面url中取参数取值的完美解决方案 -
suixinsuoyu12519:
你好,请问 “Update的系统内部执行情况可以参照附文:对u ...
Update优化小结 -
04023129:
试下 把wc.preSharedKey = "pa ...
设置 Android wifi连接 samples【转】
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.
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.
Tags: Android, Databases, howto, Java, SQLite
This entry was posted on Tuesday, March 3rd, 2009 at 2:22 pm and is filed under Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
发表评论
-
如何在Mac OS中安装 wget
2012-07-09 22:50 28486刚入手Macbook pro。说实话日本的比国内的真的便宜好的 ... -
Android开发片段 WebView
2012-02-06 14:55 1147/** Called when the activity i ... -
Office文档在线查看解决方案
2012-02-03 12:57 4668本文使用的是office软件自带的Save as 功能 自动存 ... -
事务添加的位置问题 工作总结
2010-07-13 10:44 854今天在公司的开发的框架上做了一个应用,才意识到一个问题,原来这 ... -
一个简单的JavaScript 框架
2009-08-25 15:38 1018这几天做新项目,做了一个简单的JS验证框架,分享一下,欢迎大家 ... -
解决EXT 与prototype 框架冲突问题
2009-08-10 11:59 1285很简单,只需要将prototype框架放在最前面就可以了 -
POI设置rightHead的Page和Date的fangfa
2009-05-19 14:36 977&6Page:&P Date:&D -
Oracle表段中的高水位线HWM
2009-05-03 09:53 1381在Oracle数据的存储中,可以把存储空间想象为一个水库, ... -
Django 小例子
2009-04-18 21:35 2254最近想学习python,尤其是Django,本来照着Step ... -
ORACLE 表和数据完全COPY
2009-04-17 15:52 960create table new_tab as ... -
Oracle pl/sql 中实现 continue
2009-02-01 16:30 3505CREATE TABLE AREAS ( RADIUS ... -
flex 3 从页面url中取参数取值的完美解决方案
2008-12-31 22:14 2263flex 3 从页面url中取参数取值的完美解决方案 1:当 ... -
ORACLE 游标的4种应用方法
2008-12-29 17:51 1200--1.普通cursor set serveroutput o ... -
oracle-merge用法详解
2008-12-11 09:04 3117Oracle9i引入了MERGE命令,你能够在一个SQL语句中 ... -
分组取最大
2008-12-10 17:09 1789--按某一字段分组取最大(小)值所在行的数据(2007-10- ... -
SQL语句学习 CASE WHEN THEN ELSE END
2008-12-10 17:06 1904比较2列的值选择最大的一列输出: table a ( ... -
★SqlServer 中 link serve的使用
2008-11-26 09:19 3540问题描述: 日 ... -
★Struts2中的Ajax实现 <s:a> Ⅱ
2008-11-19 10:05 3211实用JSON在Struts2中实现AJax这篇文章是抄袭Max ... -
★Struts2中的Ajax实现 <s:a>
2008-11-19 09:25 2925项目作完了,现在没什么事情做,继续研究一下Struts2框架. ... -
★给和我一样的新手推荐一个Struts2学习的网站
2008-11-19 09:19 1100给和我一样的新手推荐一个Struts2学习的网站ht ...
相关推荐
在Android系统下面,使用现有自己的数据库!哈!!!!!!!
Android 使用 AudioRecord 和 AudioTrack 完成音频PCM数据的采集和播放,并读写音频wav文件。 封装好的Java代码,可同时录制...PCM是原始音频数据,WAV是windows中常见的音频格式,只是在pcm数据中添加了一个文件头。
主要为大家详细介绍了Android存储字符串数据到txt文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
将ini文件存在android项目中assets文件夹中,以便于软件开启时自动读取ini系统配置文件。
Android本地数据存储中的文件存储,写入,重写和删除Android中.txt格式的文件
Android采用了一种不同的系统,在Android中,所有的应用软件数据(包括文件)为该应用软件所私有。然而,Android同样也提供了一种标准方式供应用软件将私有数据开放给其他应用软件。这一章节描述一个应用软件存储和...
利用SQLite Expert工具将Excel表中的数据导入android数据库.db文件中(包内包含1.SQLite Expert 工具解压可用 2. 具体导入流程有截图)。
解决Android中基于Socket方式的文件上传中出现的问题. 图片上传到服务器中. 在浏览器中测试可以通过。 证明服务器端没有问题. 但在android端上传时不行.
android手机怎样通过蓝牙发送文件和接受文件的方法
原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。
要理解Android的文件存储,首先要明白Android是如何用什么方式获取和存入数据的。Android的文件操作都是通过流的方式进行的,即IO。Android的文件操作跟Java是一样的,文件操作分为输入流(InputStream)和输出流...
教程说明见 http://www.linuxidc.com/Linux/2012-01/51441.htm
android 使用SharedPreferences对数据存储/删除/读取/文件删除
Android开发时,需要把数据存储在Excel文件中,并且用时还可以读取。支持大数据的存储和读取。
使用了一个简单的例子来测试在安卓中利用Jsoup,非常简单,解决很多小白的问题。 1.在lib中倒入jsoup-1.10.2.jar,在Android Studio中切换到Project目录下,将jsoup-1.10.2.jar放入lib,然后右键点击Add As Library...
Android 3种数据保存(SharedPreferences存储 内部文件存储 数据库存储),如项目包有问题请联系我谢谢
Android创建文件并读写数据,(本人亲测)友友们我会更新更多的源码和大家分享,希望大家喜欢 .
android ini文件 sharePrefernces 保存数据
Android不需要文件存储权限将文件保存至手机,Android不需要文件存储权限将文件保存至手机
Android studio 实现数据在数据库中的增删改查的java文件