package com.asc.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* 数据库公共类,提供基本数据库操作
*
* @author raymon
*
*/
public class DBManager {
// 默认数据库
private static final String DB_NAME = "asc.db";
// 数据库版本
private static final int DB_VERSION = 1;
// 执行open()打开数据库时,保存返回的数据库对象
private SQLiteDatabase mSQLiteDatabase = null;
// 由SQLiteOpenHelper继承过来
private DatabaseHelper mDatabaseHelper = null;
// 本地Context对象
private Context mContext = null;
private static DBManager dbConn= null;
// 查询游标对象
private Cursor cursor;
/**
* SQLiteOpenHelper内部类
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
// 当调用getWritableDatabase()或 getReadableDatabase()方法时,创建一个数据库
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE ad_record(id PRIMARY KEY NOT NULL, adUrl TEXT, apMac TEXT, createDate DATETIME);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS ad_record");
onCreate(db);
}
}
/**
* 构造函数
*
* @param mContext
*/
private DBManager(Context mContext) {
super();
this.mContext = mContext;
}
public static DBManager getInstance(Context mContext){
if (null == dbConn) {
dbConn = new DBManager(mContext);
}
return dbConn;
}
/**
* 打开数据库
*/
public void open() {
mDatabaseHelper = new DatabaseHelper(mContext);
mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
}
/**
* 关闭数据库
*/
public void close() {
if (null != mDatabaseHelper) {
mDatabaseHelper.close();
}
if (null != cursor) {
cursor.close();
}
}
/**
* 插入数据
* @param tableName 表名
* @param nullColumn null
* @param contentValues 名值对
* @return 新插入数据的ID,错误返回-1
* @throws Exception
*/
public long insert(String tableName, String nullColumn,
ContentValues contentValues) throws Exception {
try {
return mSQLiteDatabase.insert(tableName, nullColumn, contentValues);
} catch (Exception e) {
throw e;
}
}
/**
* 通过主键ID删除数据
* @param tableName 表名
* @param key 主键名
* @param id 主键值
* @return 受影响的记录数
* @throws Exception
*/
public long delete(String tableName, String key, int id) throws Exception {
try {
return mSQLiteDatabase.delete(tableName, key + " = " + id, null);
} catch (Exception e) {
throw e;
}
}
/**
* 查找表的所有数据
* @param tableName 表名
* @param columns 如果返回所有列,则填null
* @return
* @throws Exception
*/
public Cursor findAll(String tableName, String [] columns) throws Exception{
try {
cursor = mSQLiteDatabase.query(tableName, columns, null, null, null, null, null);
cursor.moveToFirst();
return cursor;
} catch (Exception e) {
throw e;
}
}
/**
* 根据主键查找数据
* @param tableName 表名
* @param key 主键名
* @param id 主键值
* @param columns 如果返回所有列,则填null
* @return Cursor游标
* @throws Exception
*/
public Cursor findById(String tableName, String key, int id, String [] columns) throws Exception {
try {
return mSQLiteDatabase.query(tableName, columns, key + " = " + id, null, null, null, null);
} catch (Exception e) {
throw e;
}
}
/**
* 根据条件查询数据
* @param tableName 表名
* @param names 查询条件
* @param values 查询条件值
* @param columns 如果返回所有列,则填null
* @param orderColumn 排序的列
* @param limit 限制返回数
* @return Cursor游标
* @throws Exception
*/
public Cursor find(String tableName, String [] names, String [] values, String [] columns, String orderColumn, String limit) throws Exception{
try {
StringBuffer selection = new StringBuffer();
for (int i = 0; i < names.length; i++) {
selection.append(names[i]);
selection.append(" = ?");
if (i != names.length - 1) {
selection.append(",");
}
}
cursor = mSQLiteDatabase.query(true, tableName, columns, selection.toString(), values, null, null, orderColumn, limit);
cursor.moveToFirst();
return cursor;
} catch (Exception e) {
throw e;
}
}
/**
*
* @param tableName 表名
* @param names 查询条件
* @param values 查询条件值
* @param args 更新列-值对
* @return true或false
* @throws Exception
*/
public boolean udpate(String tableName, String [] names, String [] values, ContentValues args) throws Exception{
try {
StringBuffer selection = new StringBuffer();
for (int i = 0; i < names.length; i++) {
selection.append(names[i]);
selection.append(" = ?");
if (i != names.length - 1) {
selection.append(",");
}
}
return mSQLiteDatabase.update(tableName, args, selection.toString(), values) > 0;
} catch (Exception e) {
throw e;
}
}
/**
* 执行sql语句,包括创建表、删除、插入
*
* @param sql
*/
public void executeSql(String sql) {
mSQLiteDatabase.execSQL(sql);
}
}
//转载请注明出处:http://forhope.iteye.com/blog/1461412
分享到:
相关推荐
手写简化适用于安卓开发中的SQLite数据库工具类,包含数据的增删改查,适用于各个需要本地数据库开发。
基于room技术封装了数据库工具类,包含DbManager类和DbHelper类,方便复用
Sqlite的C#通用类和Sqlite工具(SQLiteExpertPersSetup.exe),直接使用
http://blog.csdn.net/qq_25412055/article/details/52414420
主要介绍了Python带动态参数功能的sqlite工具类,涉及Python针对sqlite数据库的连接、查询、sql语句执行等相关操作封装与使用技巧,需要的朋友可以参考下
封装了SQLite的基本操作,只需要将源码导入工程,修改相应的字段名称,就能直接调用相应的封装好的方法。数据库的使用就是如此简单。
NULL 博文链接:https://ghostfromheaven.iteye.com/blog/976680
该资源包含操作sqlite数据库的jar包和工具类。只需将jar包导入你的工程,将工具类考入你的工程就可操作指定sqlite数据库,无需配置环境。
Sqlite 操作类 C# .net,嵌入式SQL //创建一个数据库文件 string datasource = @"d:/ChinaPower.s3db"; // System.Data.SQLite.SQLiteConnection.CreateFile(datasource); //连接数据库 System.Data....
主要用于Android开发时,本地SQLite数据库的管理工具。
SqLite数据库操作工具,可以很方便的打开Sqlite数据库,并查询相关的信息。
Unity3d Sqlite数据库 sqlite的DLL文件,在Unity安装目录可以找到。
将url自动编码
一些小型的应用程序需要使用到数据库,sqlite可以说是最好的选择。这里个人整理了一般简单通用的操作类SQLiteHelper
java操作SQLite数据库,便于嵌入式或内存数据库开发应用
非常好用的sqlite数据库管理工具、带.net调用库和java驱动包
SQLite 增删改查的工具类带demo 马上掌握知识点
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString); connectionString中包含了数据库的一些配置信息,比如数据库文件,数据库打开的密码等,可以利用System....
sqlLite 工具非常好用,建议在下载sqllite 注意引用System.Data.SQLite.dll