1、开篇废话几句,Android现有流行的Ormlite,greenDao等数据库存储框架已经很完善,但我为什么还要自己封装数据库读写呢?原因很简单,当业务需要你缩减Apk包大小的时候,你可能需要考虑很多方面的因素,例如:res资源的再利用、图片的像素降低、背景采用xml编写、assets下字体缩减,js、代码混淆,代码重构优化、layout的复用、jar包中很多类的相同(例如okhttp,Picasso)等有关网络请求的jar包很多用HttpRequset的类,作为引用者无法删减。
2、为了更好的使apk缩减,简单的封装Sqlite读写,包含增删改查,已经能满足我们日常的用途,而且减少很多不必要的类,个人感觉还是很好用的
package com.xxx.framework.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import com.xxx.framework.log.GCLogger;
import com.xxx.framework.log.Module;
import com.xxx.xxx.base.framework.utils.TelephonyInfoUtil;
import com.xxx.se.BaseApplication;
import java.util.List;
/**
* 数据库操作Helper
*
* @author zhanglong
*/
public class DBHelper extends SQLiteOpenHelper {
/**
* 注释内容
*/
public static final String TAG = "DBHelper";
/**
* 数据库名称
*/
private static final String DATABASE_NAME_STR = "genexcloud";
/**
* 数据库后缀名
*/
private static final String DATABASE_NAME_SUFFIX = ".db";
/**
* 数据库版本号(为了在W3发布,beta版能够覆盖稳定版,数据库版本跳到13开始,稳定版W3为12)
* 路测版本库增加了字段
* 1.6版本19
*/
private static final int DB_VERSION = 23;
public SQLiteDatabase db;
private static DBHelper mInstance = null;
public synchronized static DBHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new DBHelper(context);
}
return mInstance;
}
/**
* 建普通库
*
* @param context
*/
public DBHelper(Context context) {
super(context, DATABASE_NAME_STR + DATABASE_NAME_SUFFIX, null, DB_VERSION);
}
/**
* 根据用户建库
*
* @param context
* @param account
*/
public DBHelper(Context context, String account) {
super(context, DATABASE_NAME_STR + "_" + account + DATABASE_NAME_SUFFIX, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
GCLogger.debug(Module.DATA_BASE, "[" + TAG + "]" + "create table");
db.execSQL(ProvinceDBImpl.getTableSql());
db.execSQL(LatLngDBImpl.getTableSql());
db.execSQL(CityDBImpl.getTableSql());
db.execSQL(CitySiteInfoDBImpl.getTableSql());
db.execSQL(CitySiteInfoLatLngDBImpl.getTableSql());
db.execSQL(CitySiteTimeDBImpl.getTableSql());
db.execSQL(HelpInfoDBImpl.getTableSql());
db.execSQL(ReplyInfoDBImpl.getTableSql());
db.execSQL(AtuDBImpl.getTableSql());
db.execSQL(AtuDetailDBImpl.getTableSql());
db.execSQL(AreaSiteInfoDBImpl.getTableSql());
db.execSQL(AppUseTimeDBImpl.getTableSql());
db.execSQL(AtuCompareTempDBImpl.getTableSql());
db.execSQL(DouDBImpl.getTableSql());
db.execSQL(NeiDBImp.getTableSql());
db.execSQL(NeiDetailDBImpl.getTableSql());
db.execSQL(SpeedTestDBImpl.getTableSql());
db.execSQL(PerceptionTestDBImpl.getTableSql());
db.execSQL(SignSuggestionDBImpl.getTableSql());
db.execSQL(RecordInfoDBImpl.getTableSql());
db.execSQL(QueryTaskTypeDBImpl.getTableSql());
db.execSQL(IndexToolbarDBImpl.getTableSql());
db.execSQL(NpoAuthorityDBImpl.getTableSql());
db.execSQL(QuotasInfoDBImpl.getTableSql());
db.execSQL(QuotasDetailDBImpl.getTableSql());
db.execSQL(TrailMapRecordImpl.getTableSql());
db.execSQL(DiffPatternDBImpl.getTableSql());
//建表
db.execSQL(PlugApkInformationDBImpl.getTableSql());
db.execSQL(PublicShareDataDBImpl.getTableSql());
db.execSQL(NetTrendDBImpl.getTableSql());
db.execSQL(SameGradeDBImpl.getTableSql());
db.execSQL(CaseSearchResultDBImpl.getTableSql());
db.execSQL(VideoCommentDBImpl.getTableSql());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < newVersion) {
if (oldVersion == 1) {
// 1.0.1版本
upgrade2(oldVersion, db);
upgrade3(oldVersion, db);
upgrade4(oldVersion, db);
upgrade5(oldVersion, db);
upgrade6(oldVersion, db);
upgrade7(oldVersion, db);
} else if (oldVersion == 2) {
// 1.0.2版本
upgrade2(oldVersion, db);
upgrade3(oldVersion, db);
upgrade4(oldVersion, db);
upgrade5(oldVersion, db);
upgrade6(oldVersion, db);
upgrade7(oldVersion, db);
} else if (oldVersion == 3) {
// 1.0.3版本
upgrade3(oldVersion, db);
upgrade4(oldVersion, db);
upgrade5(oldVersion, db);
upgrade6(oldVersion, db);
upgrade7(oldVersion, db);
} else if (oldVersion == 4) {
// 1.0.4版本
upgrade4(oldVersion, db);
upgrade5(oldVersion, db);
upgrade6(oldVersion, db);
upgrade7(oldVersion, db);
} else if (oldVersion == 5) {
// 1.0.5版本,newVersion=6,oldVersion=5
upgrade5(oldVersion, db);
upgrade6(oldVersion, db);
upgrade7(oldVersion, db);
upgrade13(oldVersion, db);
upgrade14(oldVersion, db);
} else if (oldVersion == 6) {
// 1.0.6版本
upgrade6(oldVersion, db);
upgrade7(oldVersion, db);
upgrade13(oldVersion, db);
upgrade14(oldVersion, db);
} else if (oldVersion == 7) {
// 2.0.0版本,newVersion=8,oldVersion=7
upgrade7(oldVersion, db);
upgrade8(oldVersion, db);
upgrade9(oldVersion, db);
upgrade13(oldVersion, db);
upgrade14(oldVersion, db);
} else if (oldVersion == 8) {
// 2.1版本,newVersion=9,oldVersion=8
upgrade8(oldVersion, db);
upgrade9(oldVersion, db);
upgrade13(oldVersion, db);
upgrade14(oldVersion, db);
} else if (oldVersion == 9 || oldVersion == 10) {
// 2.3版本,newVersion=11,oldVersion=9
upgrade9(oldVersion, db);
upgrade13(oldVersion, db);
upgrade14(oldVersion, db);
} else if (oldVersion == 12) {
// W3上第一版,数据库版本号为12;beta版本不会出现12
db.execSQL(IndexToolbarDBImpl.getTableSql());
db.execSQL(NpoAuthorityDBImpl.getTableSql());
if (oldVersion > 6) {
if (!isExistField(db, "T_Dou", "totalNetDou")) {
db.execSQL("alter table T_Dou add totalNetDou double");
}
if (!isExistField(db, "T_Dou", "totalNetDataArpu")) {
db.execSQL("alter table T_Dou add totalNetDataArpu double");
}
}
upgrade13(oldVersion, db);
upgrade14(oldVersion, db);
upgrade15(oldVersion, db);
upgrade16(oldVersion, db);
upgrade18(oldVersion, db);
upgrade19(oldVersion, db);
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 13) {
upgrade13(oldVersion, db);
upgrade14(oldVersion, db);
upgrade15(oldVersion, db);
upgrade16(oldVersion, db);
upgrade18(oldVersion, db);
upgrade19(oldVersion, db);
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 14) {
upgrade14(oldVersion, db);
upgrade15(oldVersion, db);
upgrade16(oldVersion, db);
upgrade18(oldVersion, db);
upgrade19(oldVersion, db);
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 15) {
upgrade15(oldVersion, db);
upgrade16(oldVersion, db);
upgrade18(oldVersion, db);
upgrade19(oldVersion, db);
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 16) {
upgrade16(oldVersion, db);
upgrade18(oldVersion, db);
upgrade19(oldVersion, db);
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 17) {
upgrade18(oldVersion, db);
upgrade19(oldVersion, db);
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 18) {
upgrade18(oldVersion, db);
upgrade19(oldVersion, db);
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 19) {
upgrade19(oldVersion, db);
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 20) {
upgrade20(oldVersion, db);
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 21) {
upgrade21(oldVersion, db);
upgrade22(oldVersion, db);
} else if (oldVersion == 22) {
upgrade22(oldVersion, db);
}
}
}
/**
* 1.0.2版本
*/
private void upgrade2(int oldVersion, SQLiteDatabase db) {
db.execSQL(AtuDBImpl.getTableSql());
db.execSQL(AtuDetailDBImpl.getTableSql());
}
/**
* 1.0.3版本
*/
private void upgrade3(int oldVersion, SQLiteDatabase db) {
db.execSQL(AreaSiteInfoDBImpl.getTableSql());
}
/**
* 1.0.4版本,newVersion=6,oldVersion=5
*/
private void upgrade4(int oldVersion, SQLiteDatabase db) {
db.execSQL(AppUseTimeDBImpl.getTableSql());
db.execSQL(AtuDBImpl.getTableSql());
db.execSQL(AtuDetailDBImpl.getTableSql());
}
/**
* 1.0.5版本,newVersion=6,oldVersion=5
*/
private void upgrade5(int oldVersion, SQLiteDatabase db) {
db.execSQL(AtuCompareTempDBImpl.getTableSql());
}
/**
* 1.0.6版本,newVersion=7,oldVersion=6
*/
private void upgrade6(int oldVersion, SQLiteDatabase db) {
if (oldVersion >= 1) {
if (!isExistField(db, "T_City", "spell")) {
db.execSQL("alter table T_City add spell text");
}
}
db.execSQL(DouDBImpl.getTableSql());
db.execSQL(NeiDBImp.getTableSql());
db.execSQL(NeiDetailDBImpl.getTableSql());
db.execSQL(SpeedTestDBImpl.getTableSql());
}
/**
* 1.0.7版本,newVersion=8,oldVersion=7
*/
private void upgrade7(int oldVersion, SQLiteDatabase db) {
// 1.0.7版本,newVersion=8,oldVersion=7
db.execSQL(PerceptionTestDBImpl.getTableSql());
db.execSQL(SignSuggestionDBImpl.getTableSql());
db.execSQL(RecordInfoDBImpl.getTableSql());
db.execSQL(IndexToolbarDBImpl.getTableSql());
if (oldVersion > 6) {
if (!isExistField(db, "T_SpeedTest", "KeyIndex")) {
db.execSQL("alter table T_SpeedTest add KeyIndex integer");
}
}
}
/**
* 1.2版本,newVersion=9,oldVersion=8
*/
private void upgrade8(int oldVersion, SQLiteDatabase db) {
if (oldVersion > 7) {
if (!isExistField(db, "T_SpeedTest", "sponsor")) {
db.execSQL("alter table T_SpeedTest add sponsor text");
}
}
db.execSQL(QueryTaskTypeDBImpl.getTableSql());
}
/**
* 2.3版本,newVersion=10,oldVersion=9
*/
private void upgrade9(int oldVersion, SQLiteDatabase db) {
db.execSQL(NpoAuthorityDBImpl.getTableSql());
if (oldVersion > 6) {
if (!isExistField(db, "T_Dou", "totalNetDou")) {
db.execSQL("alter table T_Dou add totalNetDou double");
}
if (!isExistField(db, "T_Dou", "totalNetDataArpu")) {
db.execSQL("alter table T_Dou add totalNetDataArpu double");
}
}
}
/**
* 2.6版本,newVersion=14,oldVersion=13
*/
private void upgrade13(int oldVersion, SQLiteDatabase db) {
if (!isExistField(db, "T_NeiDetail", "remark")) {
db.execSQL("alter table T_NeiDetail add remark text");
}
if (!isExistField(db, "T_NeiDetail", "selfTestState")) {
db.execSQL("alter table T_NeiDetail add selfTestState Integer");
}
if (!isExistField(db, "T_NeiDetail", "isSoc")) {
db.execSQL("alter table T_NeiDetail add isSoc Integer");
}
if (!isExistField(db, "T_NeiDetail", "isSpeciality")) {
db.execSQL("alter table T_NeiDetail add isSpeciality Integer");
}
}
/**
* 1.3 beta,newVersion=15,oldVersion=14
*/
private void upgrade14(int oldVersion, SQLiteDatabase db) {
}
/**
* 1.4 beta,newVersion=16,oldVersion=15
*/
private void upgrade15(int oldVersion, SQLiteDatabase db) {
db.execSQL(QuotasInfoDBImpl.getTableSql());
db.execSQL(QuotasDetailDBImpl.getTableSql());
db.execSQL(TrailMapRecordImpl.getTableSql());
db.execSQL(DiffPatternDBImpl.getTableSql());
}
/**
* 1.5 beta,newVersion=17,oldVersion=16
*/
private void upgrade16(int oldVersion, SQLiteDatabase db) {
}
/**
* 1.6 beta,newVersion=19,oldVersion=18
* 涉及路测点历史记录数据库
*/
private void upgrade18(int oldVersion, SQLiteDatabase db) {
//1.1添加路测历史库字段
db.execSQL(TrailMapRecordImpl.getTableSql());
if (!isExistField(db, "T_TrailMapInfo", "loc_type")) {
db.execSQL("alter table T_TrailMapInfo add loc_type text");
}
if (!isExistField(db, "T_TrailMapInfo", "up_state")) {
db.execSQL("alter table T_TrailMapInfo add up_state integer");
}
ContentValues values = new ContentValues();
values.put("up_state", 0);//将新字段更新为未上传
values.put("loc_type", "unknown");//将定位类型更新为未知来源
values.put("netOper", TelephonyInfoUtil.getCarrier(BaseApplication.getAppContext()));//因之前版本该字段未赋值,考虑普遍情况,默认升级中未更换卡
db.update("T_TrailMapInfo", values, null, null);
//1.2其他关于插件的语句
if (oldVersion > 12) {
//NPO表添加省和市拼音
if (!isExistField(db, "T_NpoAuthority", "provice_spell")) {
db.execSQL("alter table T_NpoAuthority add provice_spell varchar(20)");
}
if (!isExistField(db, "T_NpoAuthority", "city_spell")) {
db.execSQL("alter table T_NpoAuthority add city_spell varchar(20)");
}
if (!isExistField(db, "T_NpoAuthority", "city_type")) {
db.execSQL("alter table T_NpoAuthority add city_type integer");
}
}
}
private void upgrade19(int oldVersion, SQLiteDatabase db) {
db.execSQL(PlugApkInformationDBImpl.getTableSql());
db.execSQL(PublicShareDataDBImpl.getTableSql());
//清空测试历史
db.execSQL("delete from T_PerceptionTest");
db.execSQL("alter table T_PerceptionTest add VMOS double");
db.execSQL("alter table T_PerceptionTest add videoPing double");
}
private void upgrade20(int oldVersion, SQLiteDatabase db) {
if (!isExistField(db, "T_Province", "_ID")) {
db.execSQL("alter table T_Province add _ID integer");
}
}
private void upgrade21(int oldVersion, SQLiteDatabase db) {
// 添加区分高德or百度类型字段
db.execSQL(TrailMapRecordImpl.getTableSql());
if (!isExistField(db, "T_TrailMapInfo", "sdk_type")) {
db.execSQL("alter table T_TrailMapInfo add sdk_type integer default 1");//默认百度
}
if (!isExistField(db, "T_TrailMapInfo", "distance")) {
db.execSQL("alter table T_TrailMapInfo add distance double default -1");//默认距离-1
}
if (!isExistField(db, "T_TrailMapInfo", "noDeBId")) {
db.execSQL("alter table T_TrailMapInfo add noDeBId integer default 0");//默认noDeBId 0
}
if (!isExistField(db, "T_TrailMapInfo", "cellId")) {
db.execSQL("alter table T_TrailMapInfo add cellId integer default 0");//默认cellId 0
}
}
private void upgrade22(int oldVersion, SQLiteDatabase db) {
db.execSQL(NetTrendDBImpl.getTableSql());
db.execSQL(SameGradeDBImpl.getTableSql());
db.execSQL(CaseSearchResultDBImpl.getTableSql());
db.execSQL(VideoCommentDBImpl.getTableSql());
if (!isExistField(db, "T_PlugApkInfo", "Position")) {
db.execSQL("alter table T_PlugApkInfo add Position integer default -255");
}
if (!isExistField(db, "T_PlugApkInfo", "IsShow")) {
db.execSQL("alter table T_PlugApkInfo add IsShow integer default 0");
}
if (!isExistField(db, "toolbar", "Position")) {
db.execSQL("alter table toolbar add Position integer default -255");
}
if (!isExistField(db, "toolbar", "IsShow")) {
db.execSQL("alter table toolbar add IsShow integer default 0");
}
}
/**
* 插入数据
*
* @param tableName 要插入的表格的名称
* @param values 要插入的数据由数据名称和数据值组成的键值对
*/
public long insert(String tableName, ContentValues values) {
long result = -1;
if (TextUtils.isEmpty(tableName) || null == values || values.size() == 0) {
return -1;
}
if (db == null || !db.isOpen()) {
db = this.getWritableDatabase();
}
while (db.isDbLockedByCurrentThread() || db.isDbLockedByOtherThreads()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + e.toString());
}
}
db.beginTransaction();
try {
result = db.insert(tableName, null, values);
db.setTransactionSuccessful();
} catch (Exception e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "insert fail,please try again " + e.toString());
} finally {
if (db != null && db.inTransaction()) {
db.endTransaction();
}
}
return result;
}
/**
* 以集合形式插入数据
*
* @param tableName 要插入的表格的名称
* @param values 要插入的数据由数据名称和数据值组成的键值对集合
*/
public boolean insert(String tableName, List
boolean result = false;
if (TextUtils.isEmpty(tableName) || null == values || values.size() == 0) {
return result;
}
if (db == null || !db.isOpen()) {
db = this.getWritableDatabase();
}
while (db.isDbLockedByCurrentThread() || db.isDbLockedByOtherThreads()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + e.toString());
}
}
GCLogger.debug(Module.DATA_BASE, "[" + TAG + "]" + "start excute");
try {
db.beginTransaction();
long ret = -1;
for (int i = 0; i < values.size(); i++) {
ret = db.insert(tableName, null, values.get(i));
if (ret == -1) {
GCLogger.error(Module.DATA_BASE,
"[" + TAG + "]" + "insert error! content = [" + values.get(i).toString() + "]");
}
}
result = true;
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "insert list ret = " + ret);
db.setTransactionSuccessful();
} catch (Exception e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "insert fail,please try again " + e.toString());
} finally {
if (db != null && db.inTransaction()) {
db.endTransaction();
}
}
return result;
}
/**
* 以集合形式替换数据
*
* @param tableName 要插入的表格的名称
* @param values 要插入的数据由数据名称和数据值组成的键值对集合
*/
public boolean replace(String tableName, List
boolean result = false;
if (TextUtils.isEmpty(tableName) || null == values || values.size() == 0) {
return result;
}
if (db == null || !db.isOpen()) {
db = this.getWritableDatabase();
}
while (db.isDbLockedByCurrentThread() || db.isDbLockedByOtherThreads()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "replace " + e.toString());
}
}
GCLogger.debug(Module.DATA_BASE, "[" + TAG + "]" + "start excute");
try {
db.beginTransaction();
long ret = -1;
for (int i = 0; i < values.size(); i++) {
ret = db.replace(tableName, null, values.get(i));
}
result = true;
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "replace ret = " + ret);
db.setTransactionSuccessful();
} catch (Exception e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "replace fail,please try again " + e.toString());
} finally {
if (db != null && db.inTransaction()) {
db.endTransaction();
}
}
return result;
}
/**
* 查询
*
* @param sql
* @param args
* @return
*/
public Cursor query(String sql, String[] args) {
if (TextUtils.isEmpty(sql)) {
return null;
}
if (db == null || !db.isOpen()) {
db = this.getWritableDatabase();
}
while (db.isDbLockedByCurrentThread() || db.isDbLockedByOtherThreads()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + e.toString());
}
}
Cursor cursor = null;
try {
db.beginTransaction();
cursor = db.rawQuery(sql, args);
db.setTransactionSuccessful();
} catch (Exception e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "query fail,please try again " + e.toString());
} finally {
if (db != null && db.inTransaction()) {
db.endTransaction();
}
}
return cursor;
}
/**
* 对数据库中数据的删除操作
*
* @param tableName 删除操作中要操作的数据表的名称
* @param where 要删除的数据中提供的条件参数的名称
* @param whereValues 要删除的数据中提供的条件参数的值
*/
public boolean delete(String tableName, String where, String[] whereValues) {
if (TextUtils.isEmpty(tableName)) {
return false;
}
if (db == null || !db.isOpen()) {
db = this.getWritableDatabase();
}
boolean result = false;
try {
db.beginTransaction();
int index = db.delete(tableName, where, whereValues);
if (index > 0) {
result = true;
}
db.setTransactionSuccessful();
} catch (Exception e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "delete fail,please try again");
} finally {
if (db != null && db.inTransaction()) {
db.endTransaction();
}
}
return result;
}
/**
* 对数据的更新操作
*
* @param tableName 是所对应的操作表
* @param values 需要更新的数据名称和值组成的键值对
* @param where 要更新的数据集的条件参数
* @param whereValuse 要更新的数据集的条件参数的值
*/
public boolean update(String tableName, ContentValues values, String where, String[] whereValuse) {
if (TextUtils.isEmpty(tableName) || null == values || values.size() == 0) {
return false;
}
if (db == null || !db.isOpen()) {
db = this.getWritableDatabase();
}
boolean result = false;
try {
db.beginTransaction();
int index = db.update(tableName, values, where, whereValuse);
if (index > 0) {
result = true;
}
db.setTransactionSuccessful();
} catch (Exception e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "update fail,please try again");
} finally {
if (db != null && db.inTransaction()) {
db.endTransaction();
}
}
return result;
}
/**
* 关闭所有链接中的数据库
*/
public void closeDatabase() {
try {
close();
} catch (Exception e) {
GCLogger.debug(Module.GC_RELEASE, "[" + TAG + "]" + "closeDatabase | closeDatabase fail");
}
}
/**
* 以sql语句形式插入数据
*
* @param sql sql语句
*/
public boolean insertSql(String sql) {
if (db == null || !db.isOpen()) {
db = this.getWritableDatabase();
}
while (db.isDbLockedByCurrentThread() || db.isDbLockedByOtherThreads()) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + e.toString());
}
}
GCLogger.debug(Module.DATA_BASE, "[" + TAG + "]" + "start excute");
boolean result = false;
try {
db.beginTransaction();
db.execSQL(sql);
result = true;
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "insertSql result = " + result);
db.setTransactionSuccessful();
} catch (Exception e) {
GCLogger.error(Module.DATA_BASE, "[" + TAG + "]" + "insertSql fail,please try again " + e.toString());
} finally {
if (db != null && db.inTransaction()) {
db.endTransaction();
}
}
return result;
}
/**
* 判断数据库中指定表的指定字段是否存在
*
* @param db
* @param strTableName 指定表名称
* @param strFieldName 执行字段名称
* @return
*/
private boolean isExistField(SQLiteDatabase db, String strTableName, String strFieldName) {
StringBuilder builder = new StringBuilder();
builder.append("name = '").append(strTableName).append("' AND sql LIKE '%").append(strFieldName).append("%'");
Cursor cursor = null;
try {
cursor = db.query("sqlite_master", null, builder.toString(), null, null, null, null);
return cursor.getCount() > 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return false;
}
}