1.将数据库文件存放在attets下的schema中
2.写一个DBHelper类继承SQLiteOpenHelper
继承该类会重写
public void onCreate(SQLiteDatabase db);
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
在onCreate方法中,一般会写一个executeSchema(db, "schema.sql");
private void executeSchema(SQLiteDatabase db, String schemaName) { BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(mContext.getAssets() .open(Configuration.DBSchemaRoot+"/"+schemaName))); String line; String buffer = ""; while ((line = in.readLine()) != null) { buffer += line; if (line.trim().endsWith(";")) { db.execSQL(buffer.replace(";", "")); buffer = ""; } } } catch (IOException e) { } finally { try { if (in != null) in.close(); } catch (IOException e) {} } }
这个方法重要用于读取数据库文件里的内容
onUpgrade:
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if(newVersion <= oldVersion) { return ; } int changeCnt = newVersion - oldVersion; for(int i = 0; i < changeCnt; i++) { String schemaName = "update"+(oldVersion+i)+"_"+(oldVersion+i+1)+".sql"; executeSchema(db, schemaName); } }
3 写一个AbstractDao类
public class AbstractDao{
private Context mContext; private SQLiteDatabase db; private static final int DATABASE_VERSION = 6; public static boolean mTransaction = false; public AbstractDao(Context context) { this.mContext = context; db = new DBHelper(mContext, Configuration.DatabaseFile, null, DATABASE_VERSION).getWritableDatabase(); } protected void execute(String sql) throws SQLException { db.execSQL(sql); } protected int delete(String table, String whereClause, String[] whereArgs) { int result = db.delete(table, whereClause, whereArgs); return result; } protected long insert(String table, String nullColumnHack, ContentValues values) throws SQLException { long result = db.insertOrThrow(table, nullColumnHack, values); return result; } protected int update(String table, ContentValues values, String whereClause, String[] whereArgs) { int result = db.update(table, values, whereClause, whereArgs); return result; } protected Cursor query(String sql, String[] selectionArgs) { return db.rawQuery(sql, selectionArgs); } protected Cursor query(String table, String[] columns, String selection, String[] selectionArgs) { return db.query(table, columns, selection, selectionArgs, null, null, null); } protected int getScalarValue(String sql, String[] selectionArgs) { int ret = 0; Cursor c = query(sql, selectionArgs); if(c != null) { if (c.getCount() > 0) { c.moveToFirst(); ret = c.getInt(0); } c.close(); } return ret; } /** * This database to determine whether there * @param tableName * @return */ public int isCreateTable(String tableName) { int count = 0; String sql = "Select Count(*) From sqlite_master Where type='table' And name='"+tableName+"'"; count = this.getScalarValue(sql,null); return count; } protected void beginTransaction() { db.beginTransaction(); } protected void commit() { db.setTransactionSuccessful(); db.endTransaction(); } protected void rollback() { db.endTransaction(); } public void dispose() { synchronized(db) { if (db != null && db.isOpen()) db.close(); } } }
4其他的类继承 AbstractDao类.
注意:在调用完dao方法后 一定要记得dispose();
发表评论
-
2012/10/29
2012-10-29 18:34 0实现了 图片上下左右滑动 放大放小 显示姓名 及 单击 双击 ... -
10.25
2012-10-25 13:52 0因为自定义 长按控件后, 图片放大放小功能无法实现 所以我把它 ... -
10.23
2012-10-23 23:48 0if (allFilesIsNotDir) { // ... -
主要类
2012-10-19 10:02 0package com.picture; import ja ... -
上下滑动 左右切换 图片放大放小
2012-10-19 08:34 0大概思路已经实现了 你自己改改不太合理的地方把. 最下面那个1 ... -
2012/10/16 左右滑动,放大放小显示姓名
2012-10-16 16:49 0左右滑动,放大放小显示姓名 下午 16:46 -
2012/10/16 左右滑动,放大放小
2012-10-16 08:57 0早上8:53 -
遍历图片
2012-10-14 23:10 0package com.mp; import java. ... -
android
2012-10-11 19:01 0//通过url 获取图片 public static b ... -
Android Log详解
2012-10-08 15:53 0android.util.Log常用的方法有以下5个:Log. ... -
android ui 事件处理
2012-09-14 10:33 0view类包含了一大堆命名类似on<..>List ... -
绘图
2012-08-23 15:15 725首先,如何获取 res 中的资源 数据包package ... -
android Paint属性
2012-08-23 14:07 928/** * Paint类介绍 ... -
Android中Bitmap和Drawable
2012-08-23 11:18 744一、相关概念1、Drawable就是一个可画的对象,其可能是 ... -
http请求方式
2012-08-21 16:12 0/** * * @param url 发送 ... -
sqlite
2012-08-21 15:15 0要在Android系统中操作SQLite数据库,是通过Andr ... -
反编译教程
2012-08-21 14:06 1082一、反编译Apk得到Java源代码首先要下载两个工具:dex2 ... -
将apk安装到模拟器中
2012-08-21 13:49 8241. 将apk 放在E:\android-sdk-window ...
相关推荐
SQLite入门与分析.doc SQLite入门与分析.doc
SQLite入门与分析(七)---浅谈SQLite的虚拟机.doc SQLite入门与分析(八)---存储模型.doc SQLite入门与分析(九)---VACUUM命令分析.doc
SQLite入门 SQLite入门 SQLite入门 SQLite入门 SQLite入门
我自己寻找整理的SQLite资料,希望对大家有帮助。清单如下: Android 数据库技术 sqlite3-基础教程 sqlite3使用详解 sqlite命令行手册(中文) ...SQLite入门与分析 SQLite数据库文件格式全面分析
sqlite教程,很好的sqlite入门教程
SQLite入门与分析 SQLite 内核进行一个完整的剖析 核心 API 的函数实现基本的数据库操作: 连接数据库,处理 SQL,遍历结果集。
android数据库经典教材SQLite入门.pdf
SQLite入门与分析(1-9). 嵌入式数据库 讲的很详细哦 大家分享
还不错的sqlite入门语法,适合入门学习,对象操作 数据操作 事务处理 内置函数等
sqlite入门实例
SQLite入门 简介,简单,易懂,比较系统!请参照!
SQLite入门与分析(四)---Page Cache之事务处理.doc SQLite入门与分析(五)---Page Cache之并发控制.doc SQLite入门与分析(六)---再谈SQLite的锁.doc
概括介绍SQLite的基础,对初学者有一定帮助。