Skip to content

Commit

Permalink
3.0.3 pub
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzue committed Jun 15, 2020
1 parent 0ea0972 commit f71c294
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 286 deletions.
14 changes: 10 additions & 4 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
SQLite的封装,适合轻量使用数据库的场景,半自动化快速创建表结构

<a href="https://github.com/kongzue/DBV3/">
<img src="https://img.shields.io/badge/Kongzue%20DBV3-3.0.2-green.svg" alt="Kongzue DB">
<img src="https://img.shields.io/badge/Kongzue%20DBV3-3.0.3-green.svg" alt="Kongzue DB">
</a>
<a href="https://bintray.com/myzchh/maven/DBV3/3.0.2/link">
<img src="https://img.shields.io/badge/Maven-3.0.2-blue.svg" alt="Maven">
<a href="https://bintray.com/myzchh/maven/DBV3/3.0.3/link">
<img src="https://img.shields.io/badge/Maven-3.0.3-blue.svg" alt="Maven">
</a>
<a href="http://www.apache.org/licenses/LICENSE-2.0">
<img src="https://img.shields.io/badge/License-Apache%202.0-red.svg" alt="License">
Expand Down Expand Up @@ -41,14 +41,14 @@ Maven仓库:
<dependency>
<groupId>com.kongzue.db</groupId>
<artifactId>dbv3</artifactId>
<version>3.0.2</version>
<version>3.0.3</version>
<type>pom</type>
</dependency>
```
Gradle:
在dependencies{}中添加引用:
```
implementation 'com.kongzue.db:dbv3:3.0.2'
implementation 'com.kongzue.db:dbv3:3.0.3'
```

## 使用方法
Expand Down Expand Up @@ -262,6 +262,11 @@ limitations under the License.
```

## 更新日志
v3.0.3:
- 部分可忽略错误日志不再输出;
- 更新表逻辑修改;
- bug修复及细节改进;

v3.0.2:
- 新增删除全表操作 deleteTable();
- 新增清空全表操作 cleanAll();
Expand Down
6 changes: 3 additions & 3 deletions dbv3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ 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

defaultConfig {
minSdkVersion 19
targetSdkVersion 29
versionCode 13
versionName "3.0.2.19"
versionCode 15
versionName "3.0.3"
}

buildTypes {
Expand Down
5 changes: 5 additions & 0 deletions dbv3/src/main/java/com/kongzue/dbv3/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
* 初始化方法
*
Expand Down
113 changes: 71 additions & 42 deletions dbv3/src/main/java/com/kongzue/dbv3/util/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("初始化数据库失败");
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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(); //结束事务
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -223,6 +236,9 @@ private List<String> getTableAllKeys(String tableName) {
result.add(s);
}
} catch (Exception e) {
if (DEBUGMODE) {
e.printStackTrace();
}
return result;
} finally {
if (c != null) {
Expand All @@ -239,10 +255,6 @@ public List<DBData> findData(String tableName, DBData findConditions, DB.SORT so
return new ArrayList<>();
}
List<DBData> 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 ");
Expand Down Expand Up @@ -291,6 +303,9 @@ public List<DBData> 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();
Expand All @@ -305,10 +320,6 @@ public long findDataCount(String tableName, DBData findData, List<String> 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 ");
Expand All @@ -335,12 +346,12 @@ public long findDataCount(String tableName, DBData findData, List<String> 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();
Expand All @@ -349,13 +360,29 @@ public long findDataCount(String tableName, DBData findData, List<String> whereC
return count;
}

public boolean delete(String tableName, DBData dbData, List<String> 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<String> whereConditions) {
if (db == null) {
error("警告:数据库未初始化");
return false;
}
if (dbData != null) {
Expand Down Expand Up @@ -389,7 +416,9 @@ public boolean delete(String tableName, DBData dbData, List<String> whereConditi
db.execSQL(sql.toString());
db.setTransactionSuccessful(); //设置事务成功完成
} catch (Exception e) {
e.printStackTrace();
if (DEBUGMODE) {
e.printStackTrace();
}
return false;
} finally {
db.endTransaction(); //结束事务
Expand All @@ -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 ";
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
}
Expand Down
Loading

0 comments on commit f71c294

Please sign in to comment.