From 4eaaa3e777f01c7d814e3f2eae80561e4d8b5086 Mon Sep 17 00:00:00 2001 From: liqi Date: Thu, 22 Feb 2018 15:22:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加数据库操作工具 --- README.md | 5 +- demo/src/main/AndroidManifest.xml | 1 + .../liqi/myutils/demo/TestOtherActivity.java | 28 +- .../myutils/demo/db/DBManagerOperation.java | 14 + .../myutils/demo/db/DatabaseTableHelper.java | 35 + .../db/OnDatabaseTableHelperListener.java | 16 + .../demo/db/TestDataBaseOperateActivity.java | 156 ++++ .../myutils/demo/db/TestDataBaseOperateP.java | 205 +++++ .../layout/test_data_base_operate_layout.xml | 367 ++++++++ .../main/res/layout/test_other_activity.xml | 5 + .../liqi/utils/db/BaseDBManagerOperation.java | 801 ++++++++++++++++++ .../com/liqi/utils/db/DataBaseTypeEnum.java | 25 + .../com/liqi/utils/db/DataBaseValuesEnum.java | 68 ++ 13 files changed, 1714 insertions(+), 12 deletions(-) create mode 100644 demo/src/main/java/com/liqi/myutils/demo/db/DBManagerOperation.java create mode 100644 demo/src/main/java/com/liqi/myutils/demo/db/DatabaseTableHelper.java create mode 100644 demo/src/main/java/com/liqi/myutils/demo/db/OnDatabaseTableHelperListener.java create mode 100644 demo/src/main/java/com/liqi/myutils/demo/db/TestDataBaseOperateActivity.java create mode 100644 demo/src/main/java/com/liqi/myutils/demo/db/TestDataBaseOperateP.java create mode 100644 demo/src/main/res/layout/test_data_base_operate_layout.xml create mode 100644 utils/src/main/java/com/liqi/utils/db/BaseDBManagerOperation.java create mode 100644 utils/src/main/java/com/liqi/utils/db/DataBaseTypeEnum.java create mode 100644 utils/src/main/java/com/liqi/utils/db/DataBaseValuesEnum.java diff --git a/README.md b/README.md index a39b71a..c9cebab 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ # MyUtils MyUtils是一个包含全方位的工具的工具项目。项目里面提供Base64编码解码工具、MD5加密工具、AES加密解码工具、SharePreference操作工具、 File文件操作工具、日期获取和计算工具、界面跳转Intent操作工具、字符串验证和数值转换操作工具、手机震动工具、系统资源操作工具、网络检测工具、 -wifi操作工具、单位换算工具、zip压缩和解压操作工具、XML解析操作工具(只支持几种指定格式)、图片加载和处理工具。 +wifi操作工具、单位换算工具、zip压缩和解压操作工具、XML解析操作工具(只支持几种指定格式)、图片加载和处理工具,数据库操作(增删改查)工具。 ### 内部远程依赖Library(已经远程依赖的Library,切勿主项目里重复依赖。) @@ -34,7 +34,7 @@ allprojects { ``` **2:依赖MyUtils**
```gradle -compile 'com.github.liqinew:myutils:V.1.0.0' +compile 'com.github.liqinew:myutils:V.1.0.1' ``` ### [点击查阅MyUtils-API文档](https://liqinew.github.io/MyUtils/) @@ -57,6 +57,7 @@ compile 'com.github.liqinew:myutils:V.1.0.0' * **ZipUtils**对象是zip压缩和解压操作工具 * **ImageLoaderUtils**对象是图片加载工具,采用image-loader框架实现加载。 * **ImageStaticDispose**对象是图片处理工具 +* **BaseDBManagerOperation**对象是数据库操作(增删改查)工具 * **XmlUtils**对象是XML解析操作工具,只支持几种指定格式。(建议下载demo查看支持的XML格式) #### 如有没有包含的工具,后续会慢慢完善添加进来。如你有好的工具也可以通过QQ:543945827推荐给我。
另外别忘记给我star哦。^_^..谢谢。 diff --git a/demo/src/main/AndroidManifest.xml b/demo/src/main/AndroidManifest.xml index 58f2c49..41c930e 100644 --- a/demo/src/main/AndroidManifest.xml +++ b/demo/src/main/AndroidManifest.xml @@ -16,6 +16,7 @@ + \ No newline at end of file diff --git a/demo/src/main/java/com/liqi/myutils/demo/TestOtherActivity.java b/demo/src/main/java/com/liqi/myutils/demo/TestOtherActivity.java index f27efd8..2b8ba27 100644 --- a/demo/src/main/java/com/liqi/myutils/demo/TestOtherActivity.java +++ b/demo/src/main/java/com/liqi/myutils/demo/TestOtherActivity.java @@ -10,6 +10,7 @@ import android.widget.Toast; import com.liqi.Logger; +import com.liqi.myutils.demo.db.TestDataBaseOperateActivity; import com.liqi.utils.ActivityUtil; import com.liqi.utils.FDUnitUtil; import com.liqi.utils.NetWorkUtil; @@ -298,7 +299,7 @@ public void onClick(View v) { * */ boolean matchMobile = Validation.matchMobile("19999999999"); - Toast.makeText(TestOtherActivity.this, "double类型12.123转换值:" + doubleToInt + "\n19999999999手机号码验证结果:" + matchMobile,Toast.LENGTH_SHORT).show(); + Toast.makeText(TestOtherActivity.this, "double类型12.123转换值:" + doubleToInt + "\n19999999999手机号码验证结果:" + matchMobile, Toast.LENGTH_SHORT).show(); } }); @@ -337,7 +338,7 @@ public void onClick(View v) { * */ String phoneModel = StaticUtility.getPhoneModel(); - Toast.makeText(TestOtherActivity.this, "手机品牌:" + phoneBrand + "<>手机型号:" + phoneModel + "<>项目版本号:" + versionString,Toast.LENGTH_SHORT).show(); + Toast.makeText(TestOtherActivity.this, "手机品牌:" + phoneBrand + "<>手机型号:" + phoneModel + "<>项目版本号:" + versionString, Toast.LENGTH_SHORT).show(); } }); @@ -355,9 +356,9 @@ public void onClick(View v) { * 网络检测 */ if (NetWorkUtil.isNetworkConnectionsOK(TestOtherActivity.this)) { - Toast.makeText(TestOtherActivity.this, "网络可用",Toast.LENGTH_SHORT).show(); + Toast.makeText(TestOtherActivity.this, "网络可用", Toast.LENGTH_SHORT).show(); } else { - Toast.makeText(TestOtherActivity.this, "网络不可用",Toast.LENGTH_SHORT).show(); + Toast.makeText(TestOtherActivity.this, "网络不可用", Toast.LENGTH_SHORT).show(); } } }); @@ -548,7 +549,7 @@ public void run() { findViewById(R.id.test_other_button15).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - String xml="\n" + + String xml = "\n" + "\n" + " \n" + "测试类型1\n" + @@ -559,20 +560,20 @@ public void onClick(View v) { "测试类型1>2\n" + "\n" + ""; - Logger.e("XML格式",xml); - List> list = XmlUtils.xmlPullObjList(xml, new String[]{"t", "v"}, new String[]{"data1","data2"}); - for (int i = 0; i > list = XmlUtils.xmlPullObjList(xml, new String[]{"t", "v"}, new String[]{"data1", "data2"}); + for (int i = 0; i < list.size(); i++) { Map map = list.get(i); String t = map.get("t"); String v1 = map.get("v"); String trim = testOtherTextview01.getText().toString(); - testOtherTextview01.setText(trim+"第"+(i+1)+"节目录解析数据:t="+t+"___v="+v1+"\n"); + testOtherTextview01.setText(trim + "第" + (i + 1) + "节目录解析数据:t=" + t + "___v=" + v1 + "\n"); } } }); //图片加载操作 - final ImageView imageView= (ImageView) findViewById(R.id.image_view); + final ImageView imageView = (ImageView) findViewById(R.id.image_view); findViewById(R.id.test_other_button16).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -584,6 +585,13 @@ public void onClick(View v) { 30); } }); + //数据库操作演示 + findViewById(R.id.test_other_button18).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ActivityUtil.getActivityUtil().startActivity(TestOtherActivity.this, TestDataBaseOperateActivity.class); + } + }); } private BaseSharePreference getBaseSharePreference() { diff --git a/demo/src/main/java/com/liqi/myutils/demo/db/DBManagerOperation.java b/demo/src/main/java/com/liqi/myutils/demo/db/DBManagerOperation.java new file mode 100644 index 0000000..64205cb --- /dev/null +++ b/demo/src/main/java/com/liqi/myutils/demo/db/DBManagerOperation.java @@ -0,0 +1,14 @@ +package com.liqi.myutils.demo.db; + +import android.content.Context; + +import com.liqi.utils.db.BaseDBManagerOperation; + +/** + * 数据库信息表业务操作对象 + */ +public class DBManagerOperation { + public static BaseDBManagerOperation BaseDBManagerOperation(Context context) { + return BaseDBManagerOperation.getBaseDBManagerOperation(new DatabaseTableHelper(context.getApplicationContext())); + } +} diff --git a/demo/src/main/java/com/liqi/myutils/demo/db/DatabaseTableHelper.java b/demo/src/main/java/com/liqi/myutils/demo/db/DatabaseTableHelper.java new file mode 100644 index 0000000..296ff9b --- /dev/null +++ b/demo/src/main/java/com/liqi/myutils/demo/db/DatabaseTableHelper.java @@ -0,0 +1,35 @@ +package com.liqi.myutils.demo.db; + + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +/** + * 创建数据库表对象 + * + * @author Liqi + */ +public class DatabaseTableHelper extends SQLiteOpenHelper implements OnDatabaseTableHelperListener{ + + public DatabaseTableHelper(Context context) { + + // 调用父类构造方法创建数据库 + super(context, TABLE_DB, null, TEST_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + // 营销订单 + db.execSQL("CREATE TABLE " + TEST_NAME + " (id integer primary key , "+TEST_CONTENT_ONE+" text, "+TEST_CONTENT_TWO+" text, "+TEST_CONTENT_THREE+" text, "+TEST_CONTENT_FOUR+" text)"); + + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + // 判断数据库是否存在,存在就删掉然后重新创建表 + db.execSQL("DROP TABLE IF EXISTS " + TEST_NAME); + onCreate(db); + } + +} \ No newline at end of file diff --git a/demo/src/main/java/com/liqi/myutils/demo/db/OnDatabaseTableHelperListener.java b/demo/src/main/java/com/liqi/myutils/demo/db/OnDatabaseTableHelperListener.java new file mode 100644 index 0000000..ea90ad7 --- /dev/null +++ b/demo/src/main/java/com/liqi/myutils/demo/db/OnDatabaseTableHelperListener.java @@ -0,0 +1,16 @@ +package com.liqi.myutils.demo.db; + +/** 数据库表名和表字段接口 + * Created by LiQi on 2017/12/6. + */ + +public interface OnDatabaseTableHelperListener { + String TABLE_DB="TEST_TABLE_DB"; + int TEST_VERSION=1; + String TEST_NAME="TEST_NAME", + TEST_CONTENT_ONE="TEST_CONTENT_ONE" + ,TEST_CONTENT_TWO="TEST_CONTENT_TWO", + TEST_CONTENT_THREE="TEST_CONTENT_THREE", + TEST_CONTENT_FOUR="TEST_CONTENT_FOUR"; + +} diff --git a/demo/src/main/java/com/liqi/myutils/demo/db/TestDataBaseOperateActivity.java b/demo/src/main/java/com/liqi/myutils/demo/db/TestDataBaseOperateActivity.java new file mode 100644 index 0000000..fc44649 --- /dev/null +++ b/demo/src/main/java/com/liqi/myutils/demo/db/TestDataBaseOperateActivity.java @@ -0,0 +1,156 @@ +package com.liqi.myutils.demo.db; + +import android.content.Context; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; + +import com.liqi.myutils.demo.R; + +import java.util.ArrayList; +import java.util.Map; + +/** + * 数据库操作演示界面 + *

+ * MVP模式 + *

+ * Created by LiQi on 2017/12/6. + */ + +public class TestDataBaseOperateActivity extends AppCompatActivity implements View.OnClickListener,TestDataBaseOperateP.OnTestDataBaseOperateListener>>{ + private TextView content; + private EditText add_key_one, add_key_two, add_key_three, add_key_four, + query_key_one, query_key_two, query_key_three, query_key_four, + update_key_one, update_key_two, update_key_three, update_key_four, + delete_key_one, delete_key_two, delete_key_three, delete_key_four; + private int queryCode; + + private Button query_key_query; + private TestDataBaseOperateP>> mPresenter; + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.test_data_base_operate_layout); + content = (TextView) findViewById(R.id.content); + add_key_one = (EditText) findViewById(R.id.add_key_one); + add_key_two = (EditText) findViewById(R.id.add_key_two); + add_key_three = (EditText) findViewById(R.id.add_key_three); + add_key_four = (EditText) findViewById(R.id.add_key_four); + query_key_one = (EditText) findViewById(R.id.query_key_one); + query_key_two = (EditText) findViewById(R.id.query_key_two); + query_key_three = (EditText) findViewById(R.id.query_key_three); + query_key_four = (EditText) findViewById(R.id.query_key_four); + update_key_one = (EditText) findViewById(R.id.update_key_one); + update_key_two = (EditText) findViewById(R.id.update_key_two); + update_key_three = (EditText) findViewById(R.id.update_key_three); + update_key_four = (EditText) findViewById(R.id.update_key_four); + delete_key_one = (EditText) findViewById(R.id.delete_key_one); + delete_key_two = (EditText) findViewById(R.id.delete_key_two); + delete_key_three = (EditText) findViewById(R.id.delete_key_three); + delete_key_four = (EditText) findViewById(R.id.delete_key_four); + findViewById(R.id.add_key_add).setOnClickListener(this); + query_key_query = (Button) findViewById(R.id.query_key_query); + query_key_query.setOnClickListener(this); + findViewById(R.id.update_key_update).setOnClickListener(this); + findViewById(R.id.delete_key_delete).setOnClickListener(this); + mPresenter=new TestDataBaseOperateP<>(this); + } + + @Override + public void presenterDataOk(ArrayList> presenterData) { + String hint = "数据库操作执行失败"; + if (null != presenterData && !presenterData.isEmpty()) { + hint = ""; + for (Map map : presenterData) { + if (!map.isEmpty()) { + for (Map.Entry entry : map.entrySet()) { + hint += "key:" + entry.getKey() + " values:" + entry.getValue() + "\n"; + } + } + } + } + content.setText("操作数据库提示信息:\n" + hint); + } + + @Override + public void presenterDataNo(int tag) { + + } + + @Override + public Context getContext() { + return this; + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + //增加 + case R.id.add_key_add: + String one = add_key_one.getText().toString().trim(); + String two = add_key_two.getText().toString().trim(); + String three = add_key_three.getText().toString().trim(); + String four = add_key_four.getText().toString().trim(); + mPresenter.write(one, two, three, four); + break; + //查询-轮流调用数据库查询工具方法 + case R.id.query_key_query: + + String queryOne = query_key_one.getText().toString().trim(); + String queryTwo = query_key_two.getText().toString().trim(); + String queryThree = query_key_three.getText().toString().trim(); + String queryFour = query_key_four.getText().toString().trim(); + queryCode++; + switch (queryCode) { + case 1: + mPresenter.allPrecisionFindByIdList(queryOne, queryTwo, queryThree, queryFour); + break; + case 2: + mPresenter.orPrecisionFindByIdList(queryOne, queryTwo, queryThree, queryFour); + break; + case 3: + mPresenter.allFuzzyFindByIdList(queryOne, queryTwo, queryThree, queryFour); + break; + case 4: + mPresenter.andFuzzyFindByIdList(queryOne, queryTwo, queryThree, queryFour); + break; + case 5: + mPresenter.allPrecisionFindByIdMap(queryOne, queryTwo, queryThree, queryFour); + break; + case 6: + mPresenter.orPrecisionFindByIdMap(queryOne, queryTwo, queryThree, queryFour); + break; + case 7: + mPresenter.allFuzzyFindByIdMap(queryOne, queryTwo, queryThree, queryFour); + break; + case 8: + mPresenter.andFuzzyFindByIdMap(queryOne, queryTwo, queryThree, queryFour); + queryCode = 0; + break; + } + query_key_query.setText("查询数据库>>" + (queryCode + 1)); + break; + //更新 + case R.id.update_key_update: + String updateOne = update_key_one.getText().toString().trim(); + String updateTwo = update_key_two.getText().toString().trim(); + String updateThree = update_key_three.getText().toString().trim(); + String updateFour = update_key_four.getText().toString().trim(); + mPresenter.update(updateOne, updateTwo, updateThree, updateFour); + break; + //删除 + case R.id.delete_key_delete: + String deleteOne = delete_key_one.getText().toString().trim(); + String deleteTwo = delete_key_two.getText().toString().trim(); + String deleteThree = delete_key_three.getText().toString().trim(); + String deleteFour = delete_key_four.getText().toString().trim(); + mPresenter.delete(deleteOne, deleteTwo, deleteThree, deleteFour); + break; + } + } +} diff --git a/demo/src/main/java/com/liqi/myutils/demo/db/TestDataBaseOperateP.java b/demo/src/main/java/com/liqi/myutils/demo/db/TestDataBaseOperateP.java new file mode 100644 index 0000000..1b56f73 --- /dev/null +++ b/demo/src/main/java/com/liqi/myutils/demo/db/TestDataBaseOperateP.java @@ -0,0 +1,205 @@ +package com.liqi.myutils.demo.db; + +import android.content.ContentValues; +import android.content.Context; +import android.text.TextUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +/** + * 数据库操作演示界面辅助对象 + * Created by LiQi on 2017/12/6. + */ + +public class TestDataBaseOperateP>> implements OnDatabaseTableHelperListener { + private OnTestDataBaseOperateListener mBaseOperateListener; + + public TestDataBaseOperateP(OnTestDataBaseOperateListener baseOperateListener) { + mBaseOperateListener = baseOperateListener; + } + + /** + * 增加-->详细请看save()注释 + */ + public void write(String contentOne, String contentTwo, String contentThree, String contentFour) { + ContentValues values = new ContentValues(); + values.put(TEST_CONTENT_ONE, contentOne); + values.put(TEST_CONTENT_TWO, contentTwo); + values.put(TEST_CONTENT_THREE, contentThree); + values.put(TEST_CONTENT_FOUR, contentFour); + + long saveCode = DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).save(TEST_NAME, values); + ArrayList> listValues = new ArrayList<>(); + Map map = new HashMap<>(); + map.put("isOkCode", saveCode != 0 ? "数据库增加成功" : "数据库增加失败"); + listValues.add(map); + mBaseOperateListener.presenterDataOk((V) listValues); + } + + /** + * 查询-->详细请看allPrecisionFindByIdList()注释 + */ + public void allPrecisionFindByIdList(String contentOne, String contentTwo, String contentThree, String contentFour) { + ArrayList strings = new ArrayList<>(); + strings.add(TEST_CONTENT_ONE); + strings.add(TEST_CONTENT_TWO); + strings.add(TEST_CONTENT_THREE); + strings.add(TEST_CONTENT_FOUR); + mBaseOperateListener.presenterDataOk((V) DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).allPrecisionFindByIdList(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour), strings)); + + } + + /** + * 查询-->详细请看orPrecisionFindByIdList()注释 + */ + public void orPrecisionFindByIdList(String contentOne, String contentTwo, String contentThree, String contentFour) { + ArrayList strings = new ArrayList<>(); + strings.add(TEST_CONTENT_ONE); + strings.add(TEST_CONTENT_TWO); + strings.add(TEST_CONTENT_THREE); + strings.add(TEST_CONTENT_FOUR); + mBaseOperateListener.presenterDataOk((V) DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).orPrecisionFindByIdList(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour), strings)); + + } + + /** + * 查询-->详细请看allFuzzyFindByIdList()注释 + */ + public void allFuzzyFindByIdList(String contentOne, String contentTwo, String contentThree, String contentFour) { + ArrayList strings = new ArrayList<>(); + strings.add(TEST_CONTENT_ONE); + strings.add(TEST_CONTENT_TWO); + strings.add(TEST_CONTENT_THREE); + strings.add(TEST_CONTENT_FOUR); + mBaseOperateListener.presenterDataOk((V) DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).allFuzzyFindByIdList(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour), strings)); + + } + + /** + * 查询-->详细请看andFuzzyFindByIdList()注释 + */ + public void andFuzzyFindByIdList(String contentOne, String contentTwo, String contentThree, String contentFour) { + ArrayList strings = new ArrayList<>(); + strings.add(TEST_CONTENT_ONE); + strings.add(TEST_CONTENT_TWO); + strings.add(TEST_CONTENT_THREE); + strings.add(TEST_CONTENT_FOUR); + mBaseOperateListener.presenterDataOk((V) DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).andFuzzyFindByIdList(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour), strings)); + + } + + /** + * 查询-->详细请看allPrecisionFindByIdMapt()注释 + */ + public void allPrecisionFindByIdMap(String contentOne, String contentTwo, String contentThree, String contentFour) { + ArrayList strings = new ArrayList<>(); + strings.add(TEST_CONTENT_ONE); + strings.add(TEST_CONTENT_TWO); + strings.add(TEST_CONTENT_THREE); + strings.add(TEST_CONTENT_FOUR); + ArrayList> listValues = new ArrayList<>(); + listValues.add(DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).allPrecisionFindByIdMap(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour), strings)); + mBaseOperateListener.presenterDataOk((V) listValues); + } + + /** + * 查询-->详细请看orPrecisionFindByIdMap()注释 + */ + public void orPrecisionFindByIdMap(String contentOne, String contentTwo, String contentThree, String contentFour) { + ArrayList strings = new ArrayList<>(); + strings.add(TEST_CONTENT_ONE); + strings.add(TEST_CONTENT_TWO); + strings.add(TEST_CONTENT_THREE); + strings.add(TEST_CONTENT_FOUR); + ArrayList> listValues = new ArrayList<>(); + listValues.add(DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).orPrecisionFindByIdMap(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour), strings)); + mBaseOperateListener.presenterDataOk((V) listValues); + } + + /** + * 查询-->详细请看allFuzzyFindByIdMap()注释 + */ + public void allFuzzyFindByIdMap(String contentOne, String contentTwo, String contentThree, String contentFour) { + ArrayList strings = new ArrayList<>(); + strings.add(TEST_CONTENT_ONE); + strings.add(TEST_CONTENT_TWO); + strings.add(TEST_CONTENT_THREE); + strings.add(TEST_CONTENT_FOUR); + ArrayList> listValues = new ArrayList<>(); + listValues.add(DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).allFuzzyFindByIdMap(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour), strings)); + mBaseOperateListener.presenterDataOk((V) listValues); + } + + /** + * 查询-->详细请看andFuzzyFindByIdMap()注释 + */ + public void andFuzzyFindByIdMap(String contentOne, String contentTwo, String contentThree, String contentFour) { + ArrayList strings = new ArrayList<>(); + strings.add(TEST_CONTENT_ONE); + strings.add(TEST_CONTENT_TWO); + strings.add(TEST_CONTENT_THREE); + strings.add(TEST_CONTENT_FOUR); + ArrayList> listValues = new ArrayList<>(); + listValues.add(DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).andFuzzyFindByIdMap(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour), strings)); + mBaseOperateListener.presenterDataOk((V) listValues); + } + + /** + * 更新--> 详细请看updateAnd()-updateOr()注释 + */ + public void update(String contentOne, String contentTwo, String contentThree, String contentFour) { + Map stringMap = new HashMap<>(); + if (!TextUtils.isEmpty(contentOne)) + stringMap.put(TEST_CONTENT_ONE, contentOne); + if (!TextUtils.isEmpty(contentTwo)) + stringMap.put(TEST_CONTENT_THREE, contentThree); + + ContentValues values = new ContentValues(); + values.put(TEST_CONTENT_ONE, contentOne); + values.put(TEST_CONTENT_TWO, contentTwo); + values.put(TEST_CONTENT_THREE, contentThree); + values.put(TEST_CONTENT_FOUR, contentFour); + + int update = DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).updateAnd(TEST_NAME, stringMap, values); + ArrayList> listValues = new ArrayList<>(); + Map map = new HashMap<>(); + map.put("isOkCode", update > 0 ? "数据库更新成功" : "数据库更新失败"); + listValues.add(map); + mBaseOperateListener.presenterDataOk((V) listValues); + } + + /** + * 删除-->详细请看deleteAnd()和deleteOr()注释 + */ + public void delete(String contentOne, String contentTwo, String contentThree, String contentFour) { + int deleteCode = DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).deleteAnd(TEST_NAME, queryMap(contentOne, contentTwo, contentThree, contentFour)); + ArrayList> listValues = new ArrayList<>(); + Map map = new HashMap<>(); + map.put("isOkCode", deleteCode != 0 ? "数据库删除成功" : "数据库删除失败"); + listValues.add(map); + mBaseOperateListener.presenterDataOk((V) listValues); + } + + private Map queryMap(String contentOne, String contentTwo, String contentThree, String contentFour) { + Map stringMap = new HashMap<>(); + if (!TextUtils.isEmpty(contentOne)) + stringMap.put(TEST_CONTENT_ONE, contentOne); + if (!TextUtils.isEmpty(contentTwo)) + stringMap.put(TEST_CONTENT_TWO, contentTwo); + if (!TextUtils.isEmpty(contentThree)) + stringMap.put(TEST_CONTENT_THREE, contentThree); + if (!TextUtils.isEmpty(contentFour)) + stringMap.put(TEST_CONTENT_FOUR, contentFour); + return stringMap; + } + + public interface OnTestDataBaseOperateListener { + void presenterDataOk(V v); + + Context getContext(); + + void presenterDataNo(int tag); + } +} diff --git a/demo/src/main/res/layout/test_data_base_operate_layout.xml b/demo/src/main/res/layout/test_data_base_operate_layout.xml new file mode 100644 index 0000000..9bb9b7b --- /dev/null +++ b/demo/src/main/res/layout/test_data_base_operate_layout.xml @@ -0,0 +1,367 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +