【玩转SQLite系列】(七)打造轻量级ORM工具类SQLiteDbUtil操作数据库

来源:互联网 发布:华通云数据与马云关系 编辑:程序博客网 时间:2024/05/22 15:56

转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53385868 
本文出自【DylanAndroid的博客】


  • 【玩转SQLite系列】文章目录 
    • 【玩转SQLite系列】(一)初识SQLite,重拾sql语句
    • 【玩转SQLite系列】(二)SQLite创建和打开数据库的三种方式
    • 【玩转SQLite系列】(三)通过sql语句操作SQLite数据库
    • 【玩转SQLite系列】(四)通过Android提供的API操作SQLite数据库
    • 【玩转SQLite系列】(五)SQLite数据库优化
    • 【玩转SQLite系列】(六)SQLite数据库应用案例实现历史搜索记录
    • 【玩转SQLite系列】(七)SQLite数据库轻量级ORM操作数据库工具类 

【玩转SQLite系列】(七)打造轻量级ORM工具类SQLiteDbUtil操作数据库

之前已经讲了SQLite数据库的各种使用和操作,我们发现,每次操作起来比较麻烦。在最后,我想到自己去封装一个轻量级的操作SQLite数据库工具类, 
类似于ORM对象关系映射型的数据库工具类,比较轻量级,可能功能不是那么的完美,毕竟只是个工具而已吗。

一.SQLiteDbUtil工具类超简单用法

  • 1.引入库文件

支持在build.gradle引入

compile 'cn.bluemobi.dylan:sqlitelibrary:0.1'
  • 1
  • 1
  • 2.支持的数据类型

支持的表字段数据类型包括:基本类型、包装类型、String类型、Date类型

    /**     * 支持的表字段数据类型包括:基本类型、包装类型、String类型、Date类型     */    String[] types = {"java.lang.Integer",            "java.lang.Double",            "java.lang.Float",            "java.lang.Long",            "java.lang.Short",            "java.lang.Byte",            "java.lang.Boolean",            "java.lang.Character",            "java.lang.String",            "java.util.Date",            "int",            "double",             "long",             "short",             "byte",             "boolean",             "char",             "float"};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 3.注意事项

    数据库给默认会将id作为主键并自增。

二.开始使用

  • 1.建立对象User->对应表
package cn.bluemobi.dylan.sqlite.mode;import java.util.Date;/** * Created by yuandl on 2016-11-21. */public class User {    private int id;    private String name;    private int age;    private Double integral;    private Date time;    private boolean flag;    public int getAge() {        return age;    }    public User setAge(int age) {        this.age = age;        return this;    }    public int getId() {        return id;    }    public User setId(int id) {        this.id = id;        return this;    }    public Double getIntegral() {        return integral;    }    public User setIntegral(Double integral) {        this.integral = integral;        return this;    }    public String getName() {        return name;    }    public User setName(String name) {        this.name = name;        return this;    }    public Date getTime() {        return time;    }    public void setTime(Date time) {        this.time = time;    }    public boolean isFlag() {        return flag;    }    public User setFlag(boolean flag) {        this.flag = flag;        return this;    }    @Override    public String toString() {        return "User{" +                "age=" + age +                ", id=" + id +                ", name='" + name + '\'' +                ", integral=" + integral +                ", time=" + time +                ", flag=" + flag +                '}';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 2.操作代码

    package cn.bluemobi.dylan.sqlite;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Toast;import java.util.Arrays;import java.util.Date;import java.util.List;import java.util.Map;import cn.bluemobi.dylan.sqlite.contacts.Contacts;import cn.bluemobi.dylan.sqlite.mode.User;import cn.bluemobi.dylan.sqlitelibrary.JavaReflectUtil;import cn.bluemobi.dylan.sqlitelibrary.SQLiteDbUtil;/*** 通过封装的工具类操作SQLite* Created by Administrator on 2016-11-19.*/public class UtilTestActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     getSupportActionBar().setTitle("通过封装的工具类操作SQLite");     setContentView(R.layout.ac_api_operate);     SQLiteDbUtil.getSQLiteDbUtil().openOrCreateDataBase(this);     Log.d(Contacts.TAG, "User类的所有信息:" + Arrays.toString(JavaReflectUtil.getAttributeNames(User.class)));     Log.d(Contacts.TAG, "User类的所有信息:" +  Arrays.toString(JavaReflectUtil.getAttributeType(User.class))); } /**  * 1.创建数据表user  * 表名 user  * *数据表user表结构字段  * 主键:id  * 名字:name  * 年龄:age:  *  * @param v  */ public void create(View v) {     SQLiteDbUtil.getSQLiteDbUtil().createTable(User.class); } /**  * 2.删除数据表user  *  * @param v  */ public void drop(View v) {     SQLiteDbUtil.getSQLiteDbUtil().drop(User.class); } /**  * 3.给user表中新增一条数据        *  * @param v  */ public void insert(View v) {     User user = new User();     user.setName("张三");     user.setAge(22);     user.setIntegral(12.03);     user.setTime(new Date());     long num = SQLiteDbUtil.getSQLiteDbUtil().insert(user);     if (num == -1) {         Toast.makeText(this, "插入失败", Toast.LENGTH_SHORT).show();     } else {         Toast.makeText(this, "成功插入到第" + num + "行", Toast.LENGTH_SHORT).show();     } } /**  * 4.修改user表中id为2的名字改成“李四” * @param v  */ public void update(View v) {     User user = new User();     user.setName("李四");     user.setAge(22);     user.setIntegral(12.03);     int num = SQLiteDbUtil.getSQLiteDbUtil().update(user, 5);     Toast.makeText(this, "修改了" + num + "行", Toast.LENGTH_SHORT).show();//        user.setName("王五");//        SQLiteDbUtil.getSQLiteDbUtil().update(user, "name='李四'"); } /**  * 5.删除user表中id为2的记录  * @param v  */ public void delete(View v) {     int num = SQLiteDbUtil.getSQLiteDbUtil().delete(User.class, 2);     Toast.makeText(this, "删除了" + num + "行", Toast.LENGTH_SHORT).show();//     SQLiteDbUtil.getSQLiteDbUtil().delete(User.class,"name='李四'"); } /**  * 6.查询数据  * @param v  */ public void query(View v) {     List<User> users = SQLiteDbUtil.getSQLiteDbUtil().query(User.class);     if (users == null) {         Log.d(Contacts.TAG, "没有数据");         return;     }     Log.d(Contacts.TAG, "util查:共" + users.size() + "个对象=" + users.toString());     String sql = "SELECT * FROM User";     List<Map<String, Object>> list = SQLiteDbUtil.getSQLiteDbUtil().rawQuery(sql);     Log.d(Contacts.TAG, "sql查:共" + list.size() + "个对象=" + list.toString()); } @Override protected void onDestroy() {     super.onDestroy(); }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129

    三.GitHub

0 0