学习笔记 SQLite数据库应用

前端之家收集整理的这篇文章主要介绍了学习笔记 SQLite数据库应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 
 
</pre>菜鸟可以进来看看一起学习,当然,大神看了如果能给出指导,那最好不过了。<p></p><p>一直以来对sqlite都只是简单的了解,没有做过什么系统的东西,正好最近有时间,就做了一个比较系统的demo练练手<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" /></p><p>代码在我的资源</p><p>有答题。收藏。错题集等功能</p><p>可以实现简单做题,收藏题,答完自动显示解析,答错自动添加到错题集等。</p><p>下面是基本框架</p><p><img src="http://img.blog.csdn.net/20150902160339039?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p>对于sqlite所必需的:第一件事就是建立一个数据库文件,然后建表。</p><p>这里有很重要的一个类:sqliteOpenHelper,此类不可为抽象类,不可实例化只能继承并重写其方法。</p><p></p><pre name="code" class="java">//建库,例子如下:
public class MyDBHelper extends sqliteOpenHelper {
	public MyDBHelper(Context context) {
		//dbname数据库名称,factory这里传null,version版本号
		super(context,dbname,factory,version);
	}
}
sqliteOpenHelper有两个方法:getReadableDatabase()和getWritableDatabase(),当他们两个其中任何一个被调用时,创建数据库

创建完之后会调用sqliteOpenHelper的onCreate(sqliteDatabase db)方法,在其中建立数据表:

//例如:
private static final String CREATE_TABLE = "CREATE TABLE [" + TABLE_NAME + "]("
								+ "[_id] INTEGER PRIMARY KEY,"
								+ "[titleContent] VERCHAR,"
								+ "[rightAnswer] VERCHAR,"
								+ "[analisys] VERCHAR,"
								+ "[A_answer] VERCHAR,"
								+ "[B_answer] VERCHAR,"
								+ "[C_answer] VERCHAR,"
								+ "[D_answer] VERCHAR);";

<span style="white-space:pre">	</span>//使用事务来管理建表,想要了解事务的功能可参看如下网址内容
<span style="white-space:pre">	<a target=_blank href="http://blog.csdn.net/w250shini11/article/details/8676556">点击打开链接</a></span>
	@Override
	public void onCreate(sqliteDatabase db) {
		db.beginTransaction();
		try {
			db.execsql(CREATE_TABLE);
			db.execsql(CREATE_ERRORTABLE);
			db.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}
	}

还有数据库版本的更新,说实话这一块我也比较模糊。。。希望有大神指点
@Override
	public void onUpgrade(sqliteDatabase db,int arg1,int arg2) {
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; 
		db.execsql(sql); 
		onCreate(db);
		
	}
这样,一个数据库就基本成型,剩下的就是增删改查的应用了
直接上代码

下面是一个数据库建库建表,以及简单操作的工具类:

package com.example.zx_demo.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;

public class DBUtils extends sqliteOpenHelper {
	
	private static sqliteDatabase db;
	
	/**数据库操作工具实体**/
	private static DBUtils instance;
	
	/**数据库名称**/
	private static final String DB_NAME = "savequestion.db";
	
	/**数据收藏表名称**/
	private static final String TABLE_NAME = "TABLE_SAVEQUESTION";
	
	/**创建收藏表**/
	private static final String CREATE_TABLE = "CREATE TABLE [" + TABLE_NAME + "]("
								+ "[_id] INTEGER PRIMARY KEY,"
								+ "[D_answer] VERCHAR);";
	
	/**数据表名称 错题**/
	public static final String TABLE_ERRORNAME = "TABLE_ERRORQUESTION";
	
	/**创建错题表**/
	private static final String CREATE_ERRORTABLE = "CREATE TABLE [" + TABLE_ERRORNAME + "]("
								+ "[_id] INTEGER PRIMARY KEY,"
								+ "[D_answer] VERCHAR);";
	
	private DBUtils(Context context) {
		super(context,DB_NAME,null,1);
	}
	
<span style="white-space:pre">	</span>//懒汉单例模式
	public static synchronized DBUtils getInstance(Context context) {
		if(instance == null) {
			instance = new DBUtils(context);
			db = instance.getWritableDatabase();
		}
		return instance;
	}

	@Override
	public void onCreate(sqliteDatabase db) {
		db.beginTransaction();
		try {
			db.execsql(CREATE_TABLE);
			db.execsql(CREATE_ERRORTABLE);
			db.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}
	}

	@Override
	public void onUpgrade(sqliteDatabase db,int arg2) {
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; 
		db.execsql(sql); 
		onCreate(db);
		
	}
	
	/**
	 * 对数据库插入数据
	 * @param table            要插入数据的表名
	 * @param nullColumnHack   null
	 * @param values		       要插入的值
	 * @return				       最近插入行的id,出错则返回-1
	 */
	public long insertData(String table,String nullColumnHack,ContentValues values) {
		/**the row ID of the newly inserted row,or -1 if an error occurred
			返回最近插入行的id,如果出错则返回-1**/
		long result = -1;
		synchronized (this) {
			if(!db.isOpen()) {
				db = instance.getWritableDatabase();
			}
			db.beginTransaction();
			try {
				result = db.insert(table,nullColumnHack,values);
				db.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				db.endTransaction();
			}
		}
		return result;
	}

	public void deleteData(String table,String whereClause,String[] whereArgs) {
		synchronized (this) {
			if(!db.isOpen()) {
				db = instance.getReadableDatabase();
			}
			db.beginTransaction();
			try {
				db.delete(table,whereClause,whereArgs);
				db.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				db.endTransaction();
			}
		}
	}

	public int updateData(String table,ContentValues values,String[] whereArgs) {
		
		return 0;
	}

	public Cursor queryData(String table,String selection,String[] selectionArgs,String orderBy) {
		Cursor c = null;
		synchronized (this) {
			if(!db.isOpen()) {
				db = instance.getReadableDatabase();
			}
			db.beginTransaction();
			try {
				c = db.query(table,selection,selectionArgs,null);
				db.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				db.endTransaction();
			}
			
		}
		return c;
	}
}
原文链接:https://www.f2er.com/sqlite/199373.html

猜你在找的Sqlite相关文章