`
chen123
  • 浏览: 11272 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

sqlite 入门

阅读更多

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();

 

 

分享到:
评论

相关推荐

    SQLite入门与分析

    在SQLite入门与分析(七)---浅谈SQLite的虚拟机.doc中,主要讲解了SQLite如何通过虚拟机执行SQL语句。SQLite的虚拟机,也称为VDBE(Virtual Database Engine),是SQLite的核心组件。它负责解析SQL语句,将其转化为一...

    SQLite入门与分析.doc

    SQLite 是一个开源的嵌入式关系数据库管理系统,由 D. Richard Hipp 在 2000 年发布。它被设计成轻量级、高可移植性的解决方案,适用于那些需要简单数据库功能的应用程序,尤其是嵌入式环境。SQLite 的特点是零配置...

    SQLite入门.doc

    ### SQLite入门知识点详解 #### 一、SQLite简介 SQLite是一种轻量级的嵌入式数据库管理系统,最初由D. Richard Hipp在2000年发布。作为一种开源解决方案,SQLite的目标是减少应用程序在管理数据时所需的开销。它以...

    SQLite入门资料大全

    我自己寻找整理的SQLite资料,希望对大家有帮助。清单如下: Android 数据库技术 sqlite3-基础教程 sqlite3使用详解 sqlite命令行手册(中文) ...SQLite入门与分析 SQLite数据库文件格式全面分析

    SQlite入门(很基础的介绍)

    ### SQLite入门知识点详解 #### 一、SQLite简介与特点 **SQLite** 是一款轻量级的数据库管理系统,因其独特的优势而被广泛应用于多种场景。它主要用于小型应用或嵌入式系统,无需安装即可使用,非常适合那些希望...

    嵌入式数据库SQLite入门与分析(1-9).

    ### 嵌入式数据库SQLite入门与分析 #### 一、SQLite简介 SQLite是一种轻量级的嵌入式关系型数据库管理系统,最初于2000年由D.Richard Hipp开发并发布。作为一种嵌入式数据库,SQLite与其他传统数据库管理系统(如...

    SQLite入门

    【SQLite入门】这篇文档主要针对初学者,详细介绍了SQLite数据库的下载、安装、编译以及在Visual Studio(VS)中的配置过程,同时给出了一个简单的测试代码。以下是详细的知识点: 1. **SQLite下载**: - SQLite是...

    android数据库经典教材SQLite入门.pdf

    ### SQLite入门:从安装到高级特性详解 #### 安装篇:轻松上手SQLite SQLite作为一款轻量级的数据库引擎,其安装过程异常简洁,尤其在Windows平台上,仅需下载一个命令行程序即可完成全部步骤。具体而言,访问...

    SQLite 入门教程

    ### SQLite入门教程精要 #### 一、基本简介与特性 SQLite是一款自含式、无需服务器、无需配置的事务型关系数据库引擎。由于其体积小巧(仅几百KB),非常适合嵌入到应用程序中,无需额外的数据库服务器。这使得...

    SQLite入门语法

    还不错的sqlite入门语法,适合入门学习,对象操作 数据操作 事务处理 内置函数等

    SQLite入门与分析.pdf

    SQLite是一种开源的嵌入式关系数据库管理系统,其设计目的是为了简化软件应用程序中的数据管理任务。SQLite不依赖于外部的数据库服务器进程,而是直接嵌入到使用它的应用程序中,并与应用程序共享同一个进程空间。...

Global site tag (gtag.js) - Google Analytics