diff --git a/.idea/gradle.xml b/.idea/gradle.xml index d291b3d..232b563 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,15 +1,21 @@ + diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..a5f05cd --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index f49eeed..10a6451 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ SQLite的封装,适合轻量使用数据库的场景,半自动化快速创建表结构 -Kongzue DB +Kongzue DB - -Maven + +Maven License @@ -41,14 +41,14 @@ Maven仓库: com.kongzue.db dbv3 - 3.0.2 + 3.0.3 pom ``` Gradle: 在dependencies{}中添加引用: ``` -implementation 'com.kongzue.db:dbv3:3.0.2' +implementation 'com.kongzue.db:dbv3:3.0.3' ``` ## 使用方法 @@ -262,6 +262,11 @@ limitations under the License. ``` ## 更新日志 +v3.0.3: +- 部分可忽略错误日志不再输出; +- 更新表逻辑修改; +- bug修复及细节改进; + v3.0.2: - 新增删除全表操作 deleteTable(); - 新增清空全表操作 cleanAll(); diff --git a/dbv3/build.gradle b/dbv3/build.gradle index 29ea1a4..9f15946 100644 --- a/dbv3/build.gradle +++ b/dbv3/build.gradle @@ -6,7 +6,7 @@ def siteUrl = 'https://github.com/kongzue/DBV3' //项目在github主页地址 def gitUrl = 'https://github.com/kongzue/DBV3.git' //Git仓库的地址 group = "com.kongzue.db"//发布aar前缀根节点 -version = "3.0.2.19"//发布aar的库版本 +version = "3.0.3"//发布aar的库版本 android { compileSdkVersion 29 @@ -14,8 +14,8 @@ android { defaultConfig { minSdkVersion 19 targetSdkVersion 29 - versionCode 13 - versionName "3.0.2.19" + versionCode 15 + versionName "3.0.3" } buildTypes { diff --git a/dbv3/src/main/java/com/kongzue/dbv3/DB.java b/dbv3/src/main/java/com/kongzue/dbv3/DB.java index 761cb72..25dcb20 100644 --- a/dbv3/src/main/java/com/kongzue/dbv3/DB.java +++ b/dbv3/src/main/java/com/kongzue/dbv3/DB.java @@ -6,6 +6,7 @@ import com.kongzue.dbv3.data.DBData; import com.kongzue.dbv3.util.DBHelper; +import java.io.File; import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -58,6 +59,10 @@ public static void init(Context context, String DBName) { DBHelper.getInstance().init(context, DBName); } + public static void init(Context context, File dbFile) { + DBHelper.getInstance().init(context, dbFile); + } + /** * 初始化方法 * diff --git a/dbv3/src/main/java/com/kongzue/dbv3/util/DBHelper.java b/dbv3/src/main/java/com/kongzue/dbv3/util/DBHelper.java index e8f0f04..2e021c0 100644 --- a/dbv3/src/main/java/com/kongzue/dbv3/util/DBHelper.java +++ b/dbv3/src/main/java/com/kongzue/dbv3/util/DBHelper.java @@ -9,6 +9,7 @@ import com.kongzue.dbv3.DB; import com.kongzue.dbv3.data.DBData; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -38,10 +39,9 @@ private DBHelper() { public void init(Context context, String dbName) { helper.context = context; helper.dbName = dbName; - helper.dbVersion = Preferences.getInstance().getInt(context, "KongzueDB", dbName + ".version"); try { - SQLiteOpenHelper sqLiteOpenHelper = new SQLiteHelperImpl(context, dbName, dbVersion); + SQLiteOpenHelper sqLiteOpenHelper = new SQLiteHelperImpl(context, dbName, 1); helper.db = sqLiteOpenHelper.getWritableDatabase(); if (db == null) { error("初始化数据库失败"); @@ -53,6 +53,22 @@ public void init(Context context, String dbName) { } } + public void init(Context context, File dbFile) { + helper.context = context; + helper.dbName = dbName; + + try { + helper.db = SQLiteDatabase.openOrCreateDatabase(dbFile, null); + if (db == null) { + error("初始化数据库失败"); + } + } catch (Exception e) { + if (DEBUGMODE) { + e.printStackTrace(); + } + } + } + public static DBHelper getInstance() { synchronized (DBHelper.class) { if (helper == null) { @@ -66,17 +82,8 @@ public SQLiteDatabase getDb() { return db; } - private int dbVersion = 0; - - public int getDbVersion() { - return dbVersion; - } - public boolean isHaveTable(String tableName) { synchronized (DBHelper.this) { - if (DBHelper.getInstance().getDbVersion() == 0) { - return false; - } if (db == null) { error("警告:数据库未初始化"); return false; @@ -94,7 +101,9 @@ public boolean isHaveTable(String tableName) { } db.setTransactionSuccessful(); //设置事务成功完成 } catch (Exception e) { - e.printStackTrace(); + if (DEBUGMODE) { + e.printStackTrace(); + } return false; } finally { if (c != null) { @@ -145,6 +154,9 @@ public boolean addData(String tableName, DBData data, boolean allowDuplicate) { db.setTransactionSuccessful(); //设置事务成功完成 return true; } catch (Exception e) { + if (DEBUGMODE) { + e.printStackTrace(); + } return false; } finally { db.endTransaction(); //结束事务 @@ -171,11 +183,11 @@ public boolean createNewTable(String tableName, DBData dbData) { db.execSQL(newTableSQLCommand); } } catch (Exception e) { - e.printStackTrace(); + if (DEBUGMODE) { + e.printStackTrace(); + } return false; } - dbVersion++; - Preferences.getInstance().set(context, "KongzueDB", dbName + ".version", dbVersion); restartDB(); return true; } @@ -204,10 +216,11 @@ public boolean updateTable(String tableName, DBData dbData) { db.execSQL(updateTableSQLCommand); } } catch (Exception e) { + if (DEBUGMODE) { + e.printStackTrace(); + } return false; } - dbVersion++; - Preferences.getInstance().set(context, "KongzueDB", dbName + ".version", dbVersion); restartDB(); return true; } @@ -223,6 +236,9 @@ private List getTableAllKeys(String tableName) { result.add(s); } } catch (Exception e) { + if (DEBUGMODE) { + e.printStackTrace(); + } return result; } finally { if (c != null) { @@ -239,10 +255,6 @@ public List findData(String tableName, DBData findConditions, DB.SORT so return new ArrayList<>(); } List result = new ArrayList<>(); - if (dbVersion == 0) { - error("数据库不存在,请先通过add()或createNewTable()来创建一个数据库"); - return result; - } StringBuffer sql = new StringBuffer("SELECT * FROM " + tableName); if (findConditions != null || whereConditions != null) { sql.append(" where "); @@ -291,6 +303,9 @@ public List findData(String tableName, DBData findConditions, DB.SORT so c.close(); } catch (Exception e) { error("查询错误:" + sql.toString()); + if (DEBUGMODE) { + e.printStackTrace(); + } } finally { if (c != null) { c.close(); @@ -305,10 +320,6 @@ public long findDataCount(String tableName, DBData findData, List whereC error("警告:数据库未初始化"); return 0; } - if (dbVersion == 0) { - error("数据库不存在,请先通过add()或createNewTable()来创建一个数据库"); - return 0; - } StringBuffer sql = new StringBuffer("SELECT * FROM " + tableName); if (findData != null || whereConditions != null) { sql.append(" where "); @@ -335,12 +346,12 @@ public long findDataCount(String tableName, DBData findData, List whereC try { c = db.rawQuery(sql.toString(), null); c.moveToFirst(); - try { - count = c.getLong(0); - } catch (Exception e) { - } - c.close(); + count = c.getLong(0); } catch (Exception e) { + //此错误忽略不计的原因是为此方法多用于添加前校验是否存在,肯定不存在的情况下这里必然抛异常,此异常完全可以忽略 +// if (DEBUGMODE) { +// e.printStackTrace(); +// } } finally { if (c != null) { c.close(); @@ -349,13 +360,29 @@ public long findDataCount(String tableName, DBData findData, List whereC return count; } - public boolean delete(String tableName, DBData dbData, List whereConditions) { + public long rawCount(String sql) { if (db == null) { error("警告:数据库未初始化"); - return false; + return 0; + } + long result = 0; + Cursor c = null; + try { + c = db.rawQuery(sql, null); + c.moveToFirst(); + result = c.getLong(0); + } catch (Exception e) { + } finally { + if (c != null) { + c.close(); + } } - if (dbVersion == 0) { - error("数据库不存在,请先通过add()或createNewTable()来创建一个数据库"); + return result; + } + + public boolean delete(String tableName, DBData dbData, List whereConditions) { + if (db == null) { + error("警告:数据库未初始化"); return false; } if (dbData != null) { @@ -389,7 +416,9 @@ public boolean delete(String tableName, DBData dbData, List whereConditi db.execSQL(sql.toString()); db.setTransactionSuccessful(); //设置事务成功完成 } catch (Exception e) { - e.printStackTrace(); + if (DEBUGMODE) { + e.printStackTrace(); + } return false; } finally { db.endTransaction(); //结束事务 @@ -406,10 +435,6 @@ public boolean update(String tableName, DBData dbData) { error("只能对已存在的数据(使用find查询出来的数据)进行修改"); return false; } - if (dbVersion == 0) { - error("数据库不存在,请先通过add()或createNewTable()来创建一个数据库"); - return false; - } db.beginTransaction(); try { String sql = "update " + tableName + " set "; @@ -428,7 +453,9 @@ public boolean update(String tableName, DBData dbData) { db.execSQL(sql); db.setTransactionSuccessful(); } catch (Exception e) { - e.printStackTrace(); + if (DEBUGMODE) { + e.printStackTrace(); + } return false; } finally { db.endTransaction(); @@ -453,7 +480,9 @@ public boolean deleteTable(String tableName) { db.execSQL("update sqlite_sequence set seq=0 where name='" + tableName + "';"); //将自增键初始化为0 db.setTransactionSuccessful(); } catch (Exception e) { - e.printStackTrace(); + if (DEBUGMODE) { + e.printStackTrace(); + } return false; } finally { db.endTransaction(); @@ -509,7 +538,7 @@ public void restartDB() { db.close(); } db = null; - SQLiteOpenHelper sqLiteOpenHelper = new SQLiteHelperImpl(context, dbName, dbVersion); + SQLiteOpenHelper sqLiteOpenHelper = new SQLiteHelperImpl(context, dbName, 1); db = sqLiteOpenHelper.getWritableDatabase(); } } diff --git a/dbv3/src/main/java/com/kongzue/dbv3/util/Preferences.java b/dbv3/src/main/java/com/kongzue/dbv3/util/Preferences.java deleted file mode 100644 index 6c7cf6e..0000000 --- a/dbv3/src/main/java/com/kongzue/dbv3/util/Preferences.java +++ /dev/null @@ -1,232 +0,0 @@ -package com.kongzue.dbv3.util; - -import android.content.Context; -import android.content.SharedPreferences; - -/** - * 本类采用单例设计模式,请使用getInstance()获取本类对象后进行使用 - * Created by Kongzue on 2017/3/28. - * Version 2.0 - * Update 2018.5.27 - */ - -public class Preferences { - - private static Preferences preferences; - private SharedPreferences sp; - - private Preferences() { - } - - public static Preferences getInstance() { - if (preferences == null) { - synchronized (Preferences.class) { - if (preferences == null) { - preferences = new Preferences(); - } - } - } - return preferences; - } - - /** - * 读取属性为String类型 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @return String - */ - public String getString(Context context, String path, String preferencesName) { - return getString(context, path, preferencesName, ""); - } - - /** - * 读取属性为Boolean类型 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @return Boolean - */ - public boolean getBoolean(Context context, String path, String preferencesName) { - return getBoolean(context, path, preferencesName, false); - } - - /** - * 读取属性为Int类型 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @return Int - */ - public int getInt(Context context, String path, String preferencesName) { - return getInt(context, path, preferencesName, 0); - } - - /** - * 读取属性为String类型 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param defaultValue 默认值 - * @return String - */ - public String getString(Context context, String path, String preferencesName, String defaultValue) { - initSharedPreferences(context, path); - String value = sp.getString(preferencesName, defaultValue); - return value; - } - - /** - * 读取属性为Boolean类型 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param defaultValue 默认值 - * @return Boolean - */ - public boolean getBoolean(Context context, String path, String preferencesName, boolean defaultValue) { - initSharedPreferences(context, path); - boolean value = sp.getBoolean(preferencesName, defaultValue); - return value; - } - - /** - * 读取属性为Int类型 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param defaultValue 默认值 - * @return Int - */ - public int getInt(Context context, String path, String preferencesName, int defaultValue) { - initSharedPreferences(context, path); - int value = sp.getInt(preferencesName, defaultValue); - return value; - } - - /** - * 写入String属性方法,占用资源较少,但不会立即生效 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param value 值 - * @return none - */ - public void set(Context context, String path, String preferencesName, String value) { - initSharedPreferences(context, path); - SharedPreferences.Editor editor = sp.edit(); - editor.putString(preferencesName, value); - editor.apply(); - } - - /** - * 写入Boolean属性方法,占用资源较少,但不会立即生效 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param value 值 - * @return none - */ - public void set(Context context, String path, String preferencesName, boolean value) { - initSharedPreferences(context, path); - SharedPreferences.Editor editor = sp.edit(); - editor.putBoolean(preferencesName, value); - editor.apply(); - } - - /** - * 写入Int属性方法,占用资源较少,但不会立即生效 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param value 值 - * @return none - */ - public void set(Context context, String path, String preferencesName, int value) { - initSharedPreferences(context, path); - SharedPreferences.Editor editor = sp.edit(); - editor.putInt(preferencesName, value); - editor.apply(); - } - - /** - * 写入String属性方法,立即生效 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param value 值 - * @return none - */ - public void commit(Context context, String path, String preferencesName, String value) { - initSharedPreferences(context, path); - SharedPreferences.Editor editor = sp.edit(); - editor.putString(preferencesName, value); - editor.commit(); - } - - /** - * 写入Boolean属性方法,立即生效 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param value 值 - * @return none - */ - public void commit(Context context, String path, String preferencesName, boolean value) { - initSharedPreferences(context, path); - SharedPreferences.Editor editor = sp.edit(); - editor.putBoolean(preferencesName, value); - editor.commit(); - } - - /** - * 写入Int属性方法,立即生效 - * - * @param context 上下文索引 - * @param path 路径 - * @param preferencesName 属性名 - * @param value 值 - * @return none - */ - public void commit(Context context, String path, String preferencesName, int value) { - initSharedPreferences(context, path); - SharedPreferences.Editor editor = sp.edit(); - editor.putInt(preferencesName, value); - editor.commit(); - } - - /** - * 清除(清空)所有属性的方法 - * - * @param context 上下文索引 - * @param path 路径 - * @return none - */ - public void cleanAll(Context context, String path) { - initSharedPreferences(context, path); - if (sp != null) { - sp.edit().clear().apply(); - } - } - - private void initSharedPreferences(Context context, String path) { - if (sp == null) { - sp = context.getApplicationContext().getSharedPreferences(path, Context.MODE_PRIVATE); - } - } - - public void initSharedPreferences(SharedPreferences sp) { - this.sp = sp; - } -} \ No newline at end of file