摘要:本文将带你了解Android应用开发之如何创建一个可使用的数据库框架项目:Android,希望本文对大家学Android有所帮助
本文将带你了解Android应用开发之如何创建一个可使用的数据库框架项目:Android,希望本文对大家学Android有所帮助
一个好的数据库模型需要符合MVC,通常在我们进入项目组之前,创立的数据库就像这样:
Java代码 public class MySQLiteHelper extends SQLiteOpenHelper public class MySQLiteHelper extends SQLiteOpenHelper
然后就在我们代码中增删查改了,每次一条sql语句,这样首先看起来不规范,而且也非常不方便,最重要的是要做重复性的工作。
==============================================================================
【规范】:
1.一个bean 什么是bean,从代码来说就是你要保存一行数据库的参数,比如保存用户,用户的id,名字,性别,电话等等,里面需要有get和set参数,整体样子可以是这样的,下面我们会仔细一步步来,以及截图上传。
2.一个Dao,Dao就是对数据库具体封装,如果有一个UserBean,那么需要一个UserDao作为桥梁联通SQLiteOpenHelper和UserBean,UserDao对于 User对象进行增删查改,同时映射到数据库进行增删查改,这样看起来是不是系统多了?
3.最后就是SQLiteOpenHelper类,增删查改。
==============================================================================
【动手】:
新建Android项目:
除了默认设置,我们新建一个User类作为一个bean:
这时候为了方便使用,我们事先导进来一个数据库表格。 我将我的数据库放在assets里面的datebase文件夹下,创建数据库表可以使用navicat直接创建,导出db
然后我建立的数据库其中user类表格结构如下:
然后建立我们的SQLiteClientDaoHelper 数据库操作类
Java代码 package com.example.core; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.SystemClock; import android.text.TextUtils; /** * 数据库操作类 * * @author Jing * */ public final class SQLiteClientDaoHelper { public static final String DATABASE_PATH = SQLiteHelperWrapper.PATH + File.separator + SQLiteHelperWrapper.DATABASE_NAME; private Context context; private SQLiteOpenHelper helper; private static SQLiteClientDaoHelper instance; private SQLiteClientDaoHelper(Context context) { this.context = context; this.helper = new SQLiteHelperWrapper(context); } public static synchronized SQLiteClientDaoHelper getInstance(Context context) { if(instance == null) { instance = new SQLiteClientDaoHelper(context); } return instance; } public Context getContext() { return context; } /** * 数据库插�? * @param table 表名 * @param values参数�? */ public long insert(String table, ContentValues values) { if (TextUtils.isEmpty(table)) throw new IllegalArgumentException("Table is not null!"); long id = -1; SQLiteDatabase database = helper.getWritableDatabase(); database.beginTransaction(); try { id = database.insert(table, null, values); database.setTransactionSuccessful(); } finally { database.endTransaction(); } return id; } /** * 数据库删�? * @param table */ public void delete(String table) { delete(table, null, null); } /** * 数据库删�? * @param table * @param whereClause * @param whereArgs */ public void delete(String table, String whereClause, String[] whereArgs) { if (TextUtils.isEmpty(table)) throw new IllegalArgumentException("Table is not null!"); SQLiteDatabase database = helper.getWritableDatabase(); database.beginTransaction(); try { database.delete(table, whereClause, whereArgs); database.setTransactionSuccessful(); } finally { database.endTransaction(); } } /** * 数据库更�? * @param table * @param values */ public void update(String table, ContentValues values) { update(table, values, null, null); } /** * 数据库更�? * @param table * @param values * @param whereClause * @param whereArgs */ public void update(String table, ContentValues values, String whereClause, String[] whereArgs) { if (TextUtils.isEmpty(table)) throw new IllegalArgumentException("Table is not null!"); SQLiteDatabase database = helper.getWritableDatabase(); database.beginTransaction(); try { database.update(table, values, whereClause, whereArgs); database.setTransactionSuccessful(); } finally { database.endTransaction(); } } /** * 单语句数据库执行 * @param sql */ public void excute(String sql) { if (TextUtils.isEmpty(sql)) throw new IllegalArgumentException("Sql is not null!"); SQLiteDatabase database = helper.getWritableDatabase(); database.beginTransaction(); try { database.execSQL(sql); database.setTransactionSuccessful(); } finally { database.endTransaction(); } } /** * 多语句数据库执行 * @param handle */ public void excute(TransactionHandle handle) { if (handle == null) throw new IllegalArgumentException("TransactionHandle is not null!"); SQLiteDatabase database = helper.getWritableDatabase(); database.beginTransaction(); try { handle.onTransactionHandle(database); database.setTransactionSuccessful(); } finally { database.endTransaction(); } } /** * 获取单个对象数据库操�? * @param sql * @param buildData * @return */ public <E> E getRaw(String sql, BuildData<E> buildData) { return getRaw(sql, null, buildData); } /** * 获取单个对象数据库操�? * @param <E> * @param sql * @param selectionArgs * @param buildData * @return */ public <E> E getRaw(String sql, String[] selectionArgs, BuildData<E> buildData) { List<E> list = this.getRawAll(sql, selectionArgs, buildData); if(list.size() > 0) { return list.get(0); } return null; } /** * 获取集合对象数据库操�? * @param sql * @param buildData * @return */ public <E> List<E> getRawAll(String sql, BuildData<E> buildData) { return getRawAll(sql, null, buildData); } /** * 获取集合对象数据库操�? * @param <E> * @param sql * @param selectionArgs * @param buildData * @return */ public <E> List<E> getRawAll(String sql, String[] selectionArgs, BuildData<E> buildData) { if (TextUtils.isEmpty(sql)) throw new IllegalArgumentException("Sql is not null!"); if (buildData == null) throw new IllegalArgumentException("BuildData is not null!"); long startTime = SystemClock.elapsedRealtime(); List<E> result = new ArrayList<E>(); SQLiteDatabase database = helper.getReadableDatabase(); Cursor cursor = database.rawQuery(sql, selectionArgs); try { if (cursor.moveToFirst()) { do { result.add(buildData.onBuildData(cursor)); } while (cursor.moveToNext()); } } catch (Exception e) { } finally { try { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } catch (Exception e) { } } return result; } /** * 获取单个对象数据库操�? * @param <E> * @param sql * @param selectionArgs * @param buildData * @return */ public <E> E get(String sql, String[] selectionArgs, BuildData<E> buildData) { List<E> list = this.getAll(sql, selectionArgs, buildData); if(list.size() > 0) { return list.get(0); } return null; } /** * 获取集合对象数据库操�? * @param <E> * @param sql * @param selectionArgs * @param buildData * @return */ public <E> List<E> getAll(String sql, String[] selectionArgs, BuildData<E> buildData) { if (TextUtils.isEmpty(sql)) throw new IllegalArgumentException("Sql is not null!"); if (buildData == null) throw new IllegalArgumentException("BuildData is not null!"); List<E> result = new ArrayList<E>(); SQLiteDatabase database = helper.getReadableDatabase(); Cursor cursor = database.rawQuery(sql, selectionArgs); try { if (cursor.moveToFirst()) { do { result.add(buildData.onBuildData(cursor)); } while (cursor.moveToNext()); } } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } return result; } /** * 获取单个对象数据库操�? * @param table * @param columns * @param selection * @param selectionArgs * @param buildData * @return */ public <E> E get(String table, String[] columns, String selection, String[] selectionArgs, BuildData<E> buildData) { return get(table, columns, selection, selectionArgs, null, null, null, buildData); } /** * 获取单个对象数据库操�? * @param table * @param columns * @param selection * @param selectionArgs * @param groupBy * @param having * @param buildData * @return */ public <E> E get(String table, String[] columns, String selection, String[] selectionArgs, String orderBy, BuildData<E> buildData) { return get(table, columns, selection, selectionArgs, null, null, orderBy, buildData); } /** * 获取单个对象数据库操�? * @param table * @param columns * @param selection * @param selectionArgs * @param groupBy * @param having * @param buildData * @return */ public <E> E get(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, BuildData<E> buildData) { return get(table, columns, selection, selectionArgs, groupBy, having, null, buildData); } /** * 获取单个对象数据库操�? * @param <E> * @param table * @param columns * @param selection * @param selectionArgs * @param groupBy * @param having * @param orderBy * @param buildData * @return */ public <E> E get(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, BuildData<E> buildData) { List<E> list = getAll(table, columns, selection, selectionArgs, groupBy, having, orderBy, buildData); if(list.size() > 0) { return list.get(0); } return null; } /** * 获取集合对象数据库操�? * @param table * @param columns * @param selection * @param selectionArgs * @param buildData * @return */ public <E> List<E> getAll(String table, String[] columns, String selection, String[] selectionArgs, BuildData<E> buildData) { return getAll(table, columns, selection, selectionArgs, null, null, null, buildData); } /** * 获取集合对象数据库操�? * @param table * @param columns * @param selection * @param selectionArgs * @param orderBy * @param buildData * @return */ public <E> List<E> getAll(String table, String[] columns, String selection, String[] selectionArgs, String orderBy, BuildData<E> buildData) { return getAll(table, columns, selection, selectionArgs, null, null, orderBy, buildData); } /** * 获取集合对象数据库操�? * @param table * @param columns * @param selection * @param selectionArgs * @param groupBy * @param having * @param buildData * @return */ public <E> List<E> getAll(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, BuildData<E> buildData) { return getAll(table, columns, selection, selectionArgs, groupBy, having, null, buildData); } /** * 获取集合对象数据库操�? * @param <E> * @param table * @param columns * @param selection * @param selectionArgs * @param groupBy * @param having * @param orderBy * @param buildData * @return */ public <E> List<E> getAll(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, BuildData<E> buildData) { if (buildData == null) throw new IllegalArgumentException("BuildData is not null!"); List<E> result = new ArrayList<E>(); SQLiteDatabase database = helper.getReadableDatabase(); Cursor cursor = database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy); try { if (cursor.moveToFirst()) { do { result.add(buildData.onBuildData(cursor)); } while (cursor.moveToNext()); } } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } return result; } /** * 获取单个对象数据库操�? * @param <E> * @param table * @param columns * @param selection * @param selectionArgs * @param groupBy * @param having * @param orderBy * @param limit * @param buildData * @return */ public <E> E get(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, BuildData<E> buildData) { List<E> list = this.getAll(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit, buildData); if(list.size() > 0) { return list.get(0); } return null; } /** * 获取集合对象数据库操�? * @param <E> * @param table * @param columns * @param selection * @param selectionArgs * @param groupBy * @param having * @param orderBy * @param limit * @param buildData * @return */ public <E> List<E> getAll(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, BuildData<E> buildData) { if (buildData == null) throw new IllegalArgumentException("BuildData is not null!"); List<E> result = new ArrayList<E>(); SQLiteDatabase database = helper.getReadableDatabase(); Cursor cursor = database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit); try { if (cursor.moveToFirst()) { do { result.add(buildData.onBuildData(cursor)); } while (cursor.moveToNext()); } } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } return result; } /** * 从数据库获取数据时的对象构建接口 * @author Jing * * @param <E> */ public interface BuildData<E> { E onBuildData(Cursor cursor); } /** * 在带事务的多语句的操作时的事务处理接�? * @author Jing * */ public interface TransactionHandle { void onTransactionHandle(SQLiteDatabase database); } /** * 对SQLiteOpenHelper进行包装, 统一�?有数据库操作 * @author Jing * */ public class SQLiteHelperWrapper extends SQLiteOpenHelper { @SuppressLint("SdCardPath") public static final String PATH = "/data/data/com.jacinter/databases"; public static final String DATABASE_NAME = "jttxl.db"; public static final int DEFAULT_DATABASE_VERSION = 1; public SQLiteHelperWrapper(Context context) { super(context, DATABASE_NAME, null, DEFAULT_DATABASE_VERSION); initDatabase(context); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } /** * 初始化数据库 * 当第�?次打�?数据库时, 验证/data/data/com.viewserver/databases中数据库是否存在 * 如果, 不存在就将assert/database/viewserver.db拷贝�?/data/data/com.viewserver/databases进行数据库初始化 * @param context * @return */ private boolean initDatabase(Context context) { InputStream input = null; FileOutputStream output = null; try { File file = new File(DATABASE_PATH); boolean isExists = file.exists(); if(isExists) { return false; } File directory = new File(SQLiteHelperWrapper.PATH); if(!isExists && !directory.exists() && !directory.mkdirs()) { throw new IOException(SQLiteHelperWrapper.PATH + " create fail!"); } if(!isExists && !file.createNewFile()) { throw new IOException(SQLiteHelperWrapper.DATABASE_NAME + " create fail!"); } input = context.getAssets().open("database" + File.separator + SQLiteHelperWrapper.DATABASE_NAME); output = new FileOutputStream(file); byte[] buffer = new byte[1024]; int read; while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } output.flush(); return true; } catch (Exception e) { } finally { if (input != null) { try { input.close(); input = null; output.close(); output = null; } catch (IOException e) { } } } return false; } } } package com.example.core;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.SystemClock;
import android.text.TextUtils;
/**
* 数据库操作类
*
* @author Jing
*
*/
public final class SQLiteClientDaoHelper {
public static final String DATABASE_PATH = SQLiteHelperWrapper.PATH + File.separator + SQLiteHelperWrapper.DATABASE_NAME;
private Context context;
private SQLiteOpenHelper helper;
private static SQLiteClientDaoHelper instance;
private SQLiteClientDaoHelper(Context context) {
this.context = context;
this.helper = new SQLiteHelperWrapper(context);
}
public static synchronized SQLiteClientDaoHelper getInstance(Context context) {
if(instance == null) {
instance = new SQLiteClientDaoHelper(context);
}
return instance;
}
public Context getContext() {
return context;
}
/**
* 数据库插�?
* @param table 表名
* @param values参数�?
*/
public long insert(String table, ContentValues values) {
if (TextUtils.isEmpty(table)) throw new IllegalArgumentException("Table is not null!");
long id = -1;
SQLiteDatabase database = helper.getWritableDatabase();
database.beginTransaction();
try {
id = database.insert(table, null, values);
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
return id;
}
/**
* 数据库删�?
* @param table
*/
public void delete(String table) {
delete(table, null, null);
}
/**
* 数据库删�?
* @param table
* @param whereClause
* @param whereArgs
*/
public void delete(String table, String whereClause, String[] whereArgs) {
if (TextUtils.isEmpty(table)) throw new IllegalArgumentException("Table is not null!");
SQLiteDatabase database = helper.getWritableDatabase();
database.beginTransaction();
try {
database.delete(table, whereClause, whereArgs);
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}
/**
* 数据库更�?
* @param table
* @param values
*/
public void update(String table, ContentValues values) {
update(table, values, null, null);
}
/**
* 数据库更�?
* @param table
* @param values
* @param whereClause
* @param whereArgs
*/
public void update(String table, ContentValues values, String whereClause, String[] whereArgs) {
if (TextUtils.isEmpty(table)) throw new IllegalArgumentException("Table is not null!");
SQLiteDatabase database = helper.getWritableDatabase();
database.beginTransaction();
try {
database.update(table, values, whereClause, whereArgs);
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}
/**
* 单语句数据库执行
* @param sql
*/
public void excute(String sql) {
if (TextUtils.isEmpty(sql)) throw new IllegalArgumentException("Sql is not null!");
SQLiteDatabase database = helper.getWritableDatabase();
database.beginTransaction();
try {
database.execSQL(sql);
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}
/**
* 多语句数据库执行
* @param handle
*/
public void excute(TransactionHandle handle) {
if (handle == null) throw new IllegalArgumentException("TransactionHandle is not null!");
SQLiteDatabase database = helper.getWritableDatabase();
database.beginTransaction();
try {
handle.onTransactionHandle(database);
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}
/**
* 获取单个对象数据库操�?
* @param sql
* @param buildData
* @return
*/
public <E> E getRaw(String sql, BuildData<E> buildData) {
return getRaw(sql, null, buildData);
}
/**
* 获取单个对象数据库操�?
* @param <E>
* @param sql
* @param selectionArgs
* @param buildData
* @return
*/
public <E> E getRaw(String sql, String[] selectionArgs, BuildData<E> buildData) {
List<E> list = this.getRawAll(sql, selectionArgs, buildData);
if(list.size() > 0) {
return list.get(0);
}
return null;
}
/**
* 获取集合对象数据库操�?
* @param sql
* @param buildData
* @return
*/
public <E> List<E> getRawAll(String sql, BuildData<E> buildData) {
return getRawAll(sql, null, buildData);
}
/**
* 获取集合对象数据库操�?
* @param <E>
* @param sql
* @param selectionArgs
* @param buildData
* @return
*/
public <E> List<E> getRawAll(String sql, String[] selectionArgs, BuildData<E> buildData) {
if (TextUtils.isEmpty(sql)) throw new IllegalArgumentException("Sql is not null!");
if (buildData == null) throw new IllegalArgumentException("BuildData is not null!");
long startTime = SystemClock.elapsedRealtime();
List<E> result = new ArrayList<E>();
SQLiteDatabase database = helper.getReadableDatabase();
Cursor cursor = database.rawQuery(sql, selectionArgs);
try {
if (cursor.moveToFirst()) {
do {
result.add(buildData.onBuildData(cursor));
} while (cursor.moveToNext());
}
} catch (Exception e) {
} finally {
try {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
} catch (Exception e) {
}
}
return result;
}
/**
* 获取单个对象数据库操�?
* @param <E>
* @param sql
* @param selectionArgs
* @param buildData
* @return
*/
public <E> E get(String sql, String[] selectionArgs, BuildData<E> buildData) {
List<E> list = this.getAll(sql, selectionArgs, buildData);
if(list.size() > 0) {
return list.get(0);
}
return null;
}
/**
* 获取集合对象数据库操�?
* @param <E>
* @param sql
* @param selectionArgs
* @param buildData
* @return
*/
public <E> List<E> getAll(String sql, String[] selectionArgs, BuildData<E> buildData) {
if (TextUtils.isEmpty(sql)) throw new IllegalArgumentException("Sql is not null!");
if (buildData == null) throw new IllegalArgumentException("BuildData is not null!");
List<E> result = new ArrayList<E>();
SQLiteDatabase database = helper.getReadableDatabase();
Cursor cursor = database.rawQuery(sql, selectionArgs);
try {
if (cursor.moveToFirst()) {
do {
result.add(buildData.onBuildData(cursor));
} while (cursor.moveToNext());
}
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return result;
}
/**
* 获取单个对象数据库操�?
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param buildData
* @return
*/
public <E> E get(String table, String[] columns, String selection,
String[] selectionArgs, BuildData<E> buildData) {
return get(table, columns, selection, selectionArgs, null, null, null, buildData);
}
/**
* 获取单个对象数据库操�?
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param buildData
* @return
*/
public <E> E get(String table, String[] columns, String selection,
String[] selectionArgs, String orderBy, BuildData<E> buildData) {
return get(table, columns, selection, selectionArgs, null, null, orderBy, buildData);
}
/**
* 获取单个对象数据库操�?
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param buildData
* @return
*/
public <E> E get(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having, BuildData<E> buildData) {
return get(table, columns, selection, selectionArgs, groupBy, having, null, buildData);
}
/**
* 获取单个对象数据库操�?
* @param <E>
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param orderBy
* @param buildData
* @return
*/
public <E> E get(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy, BuildData<E> buildData) {
List<E> list = getAll(table, columns, selection, selectionArgs, groupBy, having, orderBy, buildData);
if(list.size() > 0) {
return list.get(0);
}
return null;
}
/**
* 获取集合对象数据库操�?
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param buildData
* @return
*/
public <E> List<E> getAll(String table, String[] columns, String selection,
String[] selectionArgs, BuildData<E> buildData) {
return getAll(table, columns, selection, selectionArgs, null, null, null, buildData);
}
/**
* 获取集合对象数据库操�?
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param orderBy
* @param buildData
* @return
*/
public <E> List<E> getAll(String table, String[] columns, String selection,
String[] selectionArgs, String orderBy, BuildData<E> buildData) {
return getAll(table, columns, selection, selectionArgs, null, null, orderBy, buildData);
}
/**
* 获取集合对象数据库操�?
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param buildData
* @return
*/
public <E> List<E> getAll(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having, BuildData<E> buildData) {
return getAll(table, columns, selection, selectionArgs, groupBy, having, null, buildData);
}
/**
* 获取集合对象数据库操�?
* @param <E>
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param orderBy
* @param buildData
* @return
*/
public <E> List<E> getAll(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy, BuildData<E> buildData) {
if (buildData == null) throw new IllegalArgumentException("BuildData is not null!");
List<E> result = new ArrayList<E>();
SQLiteDatabase database = helper.getReadableDatabase();
Cursor cursor = database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
try {
if (cursor.moveToFirst()) {
do {
result.add(buildData.onBuildData(cursor));
} while (cursor.moveToNext());
}
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return result;
}
/**
* 获取单个对象数据库操�?
* @param <E>
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param orderBy
* @param limit
* @param buildData
* @return
*/
public <E> E get(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy, String limit, BuildData<E> buildData) {
List<E> list = this.getAll(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit, buildData);
if(list.size() > 0) {
return list.get(0);
}
return null;
}
/**
* 获取集合对象数据库操�?
* @param <E>
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param orderBy
* @param limit
* @param buildData
* @return
*/
public <E> List<E> getAll(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy, String limit, BuildData<E> buildData) {
if (buildData == null) throw new IllegalArgumentException("BuildData is not null!");
List<E> result = new ArrayList<E>();
SQLiteDatabase database = helper.getReadableDatabase();
Cursor cursor = database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
try {
if (cursor.moveToFirst()) {
do {
result.add(buildData.onBuildData(cursor));
} while (cursor.moveToNext());
}
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return result;
}
/**
* 从数据库获取数据时的对象构建接口
* @author Jing
*
* @param <E>
*/
public interface BuildData<E> {
E onBuildData(Cursor cursor);
}
/**
* 在带事务的多语句的操作时的事务处理接�?
* @author Jing
*
*/
public interface TransactionHandle {
void onTransactionHandle(SQLiteDatabase database);
}
/**
* 对SQLiteOpenHelper进行包装, 统一�?有数据库操作
* @author Jing
*
*/
public class SQLiteHelperWrapper extends SQLiteOpenHelper {
@SuppressLint("SdCardPath")
public static final String PATH = "/data/data/com.jacinter/databases";
public static final String DATABASE_NAME = "jttxl.db";
public static final int DEFAULT_DATABASE_VERSION = 1;
public SQLiteHelperWrapper(Context context) {
super(context, DATABASE_NAME, null, DEFAULT_DATABASE_VERSION);
initDatabase(context);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* 初始化数据库
* 当第�?次打�?数据库时, 验证/data/data/com.viewserver/databases中数据库是否存在
* 如果, 不存在就将assert/database/viewserver.db拷贝�?/data/data/com.viewserver/databases进行数据库初始化
* @param context
* @return
*/
private boolean initDatabase(Context context) {
InputStream input = null;
FileOutputStream output = null;
try {
File file = new File(DATABASE_PATH);
boolean isExists = file.exists();
if(isExists) {
return false;
}
File directory = new File(SQLiteHelperWrapper.PATH);
if(!isExists && !directory.exists() && !directory.mkdirs()) {
throw new IOException(SQLiteHelperWrapper.PATH + " create fail!");
}
if(!isExists && !file.createNewFile()) {
throw new IOException(SQLiteHelperWrapper.DATABASE_NAME + " create fail!");
}
input = context.getAssets().open("database" + File.separator + SQLiteHelperWrapper.DATABASE_NAME);
output = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int read;
while ((read = input
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号