Skip to content

Commit

Permalink
3.0.3.4
Browse files Browse the repository at this point in the history
- fix bugs;
- add addOrUpdate function.
  • Loading branch information
kongzue committed Jan 15, 2021
1 parent 904d2c4 commit 2d953f7
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 301 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

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

1 change: 1 addition & 0 deletions .idea/gradle.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

16 changes: 16 additions & 0 deletions app/src/main/java/com/kongzue/dbv3demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MainActivity extends AppCompatActivity {
private Button btnAddDemoData;
private Button btnAddJsonData;
private Button btnAddMapData;
private Button btnAddOrUpdate;
private EditText editName;
private EditText editAge;
private RadioGroup rgpGender;
Expand Down Expand Up @@ -67,6 +68,7 @@ protected void onCreate(Bundle savedInstanceState) {
btnAddDemoData = findViewById(R.id.btn_add_demo_data);
btnAddJsonData = findViewById(R.id.btn_add_json_data);
btnAddMapData = findViewById(R.id.btn_add_map_data);
btnAddOrUpdate = findViewById(R.id.btn_add_or_update);
editName = findViewById(R.id.edit_name);
editAge = findViewById(R.id.edit_age);
rgpGender = findViewById(R.id.rgp_gender);
Expand Down Expand Up @@ -319,6 +321,20 @@ public void onClick(View v) {
logAllData();
}
});

btnAddOrUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map map = new HashMap<>();
map.put("username", "张三");
map.put("age", 99);
map.put("gender", 1);
map.put("isVIP", false);

DB.getTable("user").addOrUpdate("username",new DBData(map));
logAllData();
}
});
}

private void addDemoData() {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
android:layout_height="wrap_content"
android:text="通过Map添加数据" />

<Button
android:id="@+id/btn_add_or_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加关键key:name的重复数据,存在则更新" />

</LinearLayout>

</HorizontalScrollView>
Expand Down
4 changes: 2 additions & 2 deletions dbv3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.3"//发布aar的库版本
version = "3.0.3.4"//发布aar的库版本

android {
compileSdkVersion 29
Expand All @@ -15,7 +15,7 @@ android {
minSdkVersion 19
targetSdkVersion 29
versionCode 15
versionName "3.0.3"
versionName "3.0.3.4"
}

buildTypes {
Expand Down
70 changes: 63 additions & 7 deletions dbv3/src/main/java/com/kongzue/dbv3/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public static void init(Context context, File dbFile) {
DBHelper.getInstance().init(context, dbFile);
}

@Deprecated
public static void init(Context context, String DBName, int oldVer) {
DBHelper.getInstance().init(context, DBName, oldVer);
}

/**
* 初始化方法
*
Expand Down Expand Up @@ -120,24 +125,75 @@ public boolean add(DBData dbData, boolean allowDuplicate) {
return true;
}

/**
* 添加新的数据,若 唯一键 存在则更新已有数据
*
* @param onlyKey 唯一键
* @param dbData 要添加的数据
* @return 是否添加成功
*/
public boolean addOrUpdate(String onlyKey, DBData dbData) {
boolean createFlag = DBHelper.getInstance().createNewTable(tableName, dbData);
if (!createFlag) {
error("严重错误:创建表失败,表模板" + dbData.getPrintTable());
return false;
}
List<DBData> findData = DBHelper.getInstance().findData(tableName, new DBData().set(onlyKey, dbData.getString(onlyKey)), SORT.ASC, null, -1, 0);
if (findData.isEmpty()) {
return add(dbData, false);
} else {
boolean flag = true;
dbData.remove("_id");
for (DBData data : findData) {
data.putAll(dbData);
if (!update(data)) {
flag = false;
}
}
return flag;
}
}


/**
* 添加新的数据,若 唯一键 存在则不添加
*
* @param onlyKey 唯一键
* @param dbData 要添加的数据
* @return 是否添加成功
*/
public boolean addWhenNon(String onlyKey, DBData dbData) {
boolean createFlag = DBHelper.getInstance().createNewTable(tableName, dbData);
if (!createFlag) {
error("严重错误:创建表失败,表模板" + dbData.getPrintTable());
return false;
}
List<DBData> findData = DBHelper.getInstance().findData(tableName, new DBData().set(onlyKey, dbData.getString(onlyKey)), SORT.ASC, null, -1, 0);
if (findData.isEmpty()) {
return add(dbData, false);
} else {
return false;
}
}

/**
* 修改一个已存在的数据
*
* @param dbData 要修改的数据
* @return 是否修改成功
*/
public boolean update(DBData dbData) {
boolean updateFlag = DBHelper.getInstance().update(tableName, dbData);
if (!updateFlag){
boolean updateFlag = DBHelper.getInstance().update(tableName, dbData);
if (!updateFlag) {
boolean updateTableFlag = DBHelper.getInstance().updateTable(tableName, dbData);
if (!updateTableFlag) {
error("严重错误:更新表失败,表模板" + dbData.getPrintTable());
return false;
}
if ( DBHelper.getInstance().update(tableName, dbData)){
if (DBHelper.getInstance().update(tableName, dbData)) {
log("表 " + tableName + " 更新数据:" + dbData);
return true;
}else{
} else {
error("严重错误:更新数据失败:" + dbData);
return false;
}
Expand Down Expand Up @@ -212,7 +268,7 @@ public boolean delete() {
*
* @return 是否删除成功
*/
public boolean cleanAll(){
public boolean cleanAll() {
return DBHelper.getInstance().delete(tableName, null, null);
}

Expand All @@ -221,7 +277,7 @@ public boolean cleanAll(){
*
* @return 是否删除成功
*/
public boolean deleteTable(){
public boolean deleteTable() {
return DBHelper.getInstance().deleteTable(tableName);
}

Expand Down Expand Up @@ -274,7 +330,7 @@ public DB subData(long start, long count) {
* @return 继续条件
*/
public DB cleanLimit() {
limitStart = 0;
limitStart = -1;
limitCount = 0;
return this;
}
Expand Down
Loading

0 comments on commit 2d953f7

Please sign in to comment.