Skip to content

Commit

Permalink
添加数据库操作工具
Browse files Browse the repository at this point in the history
添加数据库操作工具
  • Loading branch information
liqi committed Feb 22, 2018
1 parent 0c7d90b commit 4eaaa3e
Show file tree
Hide file tree
Showing 13 changed files with 1,714 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# MyUtils
MyUtils是一个包含全方位的工具的工具项目。项目里面提供Base64编码解码工具、MD5加密工具、AES加密解码工具、SharePreference操作工具、
File文件操作工具、日期获取和计算工具、界面跳转Intent操作工具、字符串验证和数值转换操作工具、手机震动工具、系统资源操作工具、网络检测工具、
wifi操作工具、单位换算工具、zip压缩和解压操作工具、XML解析操作工具(只支持几种指定格式)、图片加载和处理工具。
wifi操作工具、单位换算工具、zip压缩和解压操作工具、XML解析操作工具(只支持几种指定格式)、图片加载和处理工具,数据库操作(增删改查)工具

### 内部远程依赖Library(已经远程依赖的Library,切勿主项目里重复依赖。)

Expand Down Expand Up @@ -34,7 +34,7 @@ allprojects {
```
**2:依赖MyUtils**<br>
```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/)
Expand All @@ -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推荐给我。<br>另外别忘记给我star哦。^_^..谢谢。
1 change: 1 addition & 0 deletions demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".db.TestDataBaseOperateActivity"/>
</application>

</manifest>
28 changes: 18 additions & 10 deletions demo/src/main/java/com/liqi/myutils/demo/TestOtherActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
});

Expand Down Expand Up @@ -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();
}
});

Expand All @@ -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();
}
}
});
Expand Down Expand Up @@ -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="<?xml version='1.0' encoding='UTF-8'?>\n" +
String xml = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<program>\n" +
" <data1>\n" +
"<t>测试类型1</t>\n" +
Expand All @@ -559,20 +560,20 @@ public void onClick(View v) {
"<v>测试类型1>2</v>\n" +
"</data2>\n" +
"</program>";
Logger.e("XML格式",xml);
List<Map<String,String>> list = XmlUtils.xmlPullObjList(xml, new String[]{"t", "v"}, new String[]{"data1","data2"});
for (int i = 0; i <list.size() ; i++) {
Logger.e("XML格式", xml);
List<Map<String, String>> list = XmlUtils.xmlPullObjList(xml, new String[]{"t", "v"}, new String[]{"data1", "data2"});
for (int i = 0; i < list.size(); i++) {
Map<String, String> 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) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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";

}
Original file line number Diff line number Diff line change
@@ -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;

/**
* 数据库操作演示界面
* <p>
* MVP模式
* </P>
* Created by LiQi on 2017/12/6.
*/

public class TestDataBaseOperateActivity extends AppCompatActivity implements View.OnClickListener,TestDataBaseOperateP.OnTestDataBaseOperateListener<ArrayList<Map<String, String>>>{
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<ArrayList<Map<String, String>>> 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<Map<String, String>> presenterData) {
String hint = "数据库操作执行失败";
if (null != presenterData && !presenterData.isEmpty()) {
hint = "";
for (Map<String, String> map : presenterData) {
if (!map.isEmpty()) {
for (Map.Entry<String, String> 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;
}
}
}
Loading

0 comments on commit 4eaaa3e

Please sign in to comment.