SQLite数据库轻量级ORM *** 作数据库工具类

SQLite数据库轻量级ORM *** 作数据库工具类,第1张

概述【玩转SQLite系列】文章目录 【玩转SQLite系列】(一)初识SQLite,重拾sql语句 【玩转SQLite系列】(二)SQLite创建和打开数据库的三种方式 【玩转SQLite系列】(三)通过sql语句 *** 作SQLite数据库 【玩转SQLite系列】(四)通过Android提供的API *** 作SQLite数据库 【玩转SQLite系列】(五)SQLite数据库优化 【玩转SQLite系列】( 【玩转sqlite系列】文章目录 【玩转sqlite系列】(一)初识sqlite,重拾SQL语句 【玩转sqlite系列】(二)sqlite创建和打开数据库的三种方式 【玩转sqlite系列】(三)通过SQL语句 *** 作sqlite数据库 【玩转sqlite系列】(四)通过AndroID提供的API *** 作sqlite数据库 【玩转sqlite系列】(五)sqlite数据库优化 【玩转sqlite系列】(六)sqlite数据库应用案例实现历史搜索记录 【玩转sqlite系列】(七)sqlite数据库轻量级ORM *** 作数据库工具类 【玩转sqlite系列】(六)sqlite数据库应用案例实现历史搜索记录 前面通过一系列的文章讲述了sqlite的各种使用场景,那么我们用一个实际的案例去实现一个搜索历史记录的功能。 这里面用到了以下内容: 【AndroID自定义view实战】之自定义超简单SearchVIEw搜索框 AndroID宽度全屏的Dialog和DialogFragment用法 Java泛型应用之打造AndroID万能VIEwHolder-超简洁写法 Java泛型应用之打造AndroID中ListVIEw和GrIDVIEw万能适配器【CommonAdapter】–超简洁写法 不了解的可以去学习一下。 一.编写一个历史搜索记录实例对象 package cn.bluemobi.dylan.sqlite; import java.util.Date; /** * 搜索记录的 *** 作对象 * Created by administrator on 2016-11-20. */ public class History { /** * ID 主键,自增 */ private int ID; /** * 搜索的内容 */ private String content; /** * 搜索的时间 */ private String time; public int getID() { return ID; } public voID setID(int ID) { this.ID = ID; } public String getContent() { return content; } public voID setContent(String content) { this.content = content; } public String getTime() { return time; } public voID setTime(String time) { this.time = time; } } 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 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 二.编写一个 *** 作数据库的管理工具类 package cn.bluemobi.dylan.sqlite; import androID.content.ContentValues; import androID.database.Cursor; import androID.database.sqlite.sqliteDatabase; import androID.os.Environment; import java.io.file; import java.util.ArrayList; import java.util.List; /** * 数据库 *** 作管理类 * Created by administrator on 2016-11-19. */ public class DBManager { private static volatile DBManager dbManager; private sqliteDatabase sqliteDatabase; private DBManager() { openDataBase(); createtable(); } public static DBManager getDBManager() { if (dbManager == null) { synchronized (DBManager.class) { if (dbManager == null) { dbManager = new DBManager(); } } } return dbManager; } /** * 数据库名称 */ private final String DATABASE_name = "info.db"; /** * 表名 */ private final String table_name = "history"; /** * 表格所包含的字段 */ private class HistoryDbColumn { /** * 字段一 ID */ public static final String ID = "ID"; /** * 字段二 内容 */ public static final String CONTENT = "name"; /** * 字段三 时间 */ public static final String TIME = "time"; } /** * 1.创建或打开数据库连接 **/ private voID openDataBase() { file dataBasefile = new file(Environment.getExternalStorageDirectory() + "/sqlite",DATABASE_name); if (!dataBasefile.getParentfile().exists()) { dataBasefile.mkdirs(); } sqliteDatabase = sqliteDatabase.openorCreateDatabase(dataBasefile,null); } /**** * 2.创建表 */ private voID createtable() { String sql = "CREATE table " + "IF NOT EXISTS " + table_name + "(" + HistoryDbColumn.ID + " Integer PRIMARY KEY autoINCREMENT," + HistoryDbColumn.CONTENT + " varchar," + HistoryDbColumn.TIME + " datetime)"; sqliteDatabase.execsql(sql); } /** * 插入一条数据 * * @param history * @return */ public long insert(History history) { ContentValues contentValues = new ContentValues(); contentValues.put(HistoryDbColumn.CONTENT,history.getContent()); contentValues.put(HistoryDbColumn.TIME,history.getTime()); long num = sqliteDatabase.insert(table_name,null,contentValues); return num; } /** * 根据ID删除一条数据 * * @param ID * @return */ public long delete(int ID) { long num = sqliteDatabase.delete(table_name,HistoryDbColumn.ID + "=?",new String[]{String.valueOf(ID)}); return num; } /** * 根据ID修改一条数据 * * @param ID * @return */ public long update(History history,int ID) { ContentValues contentValues = new ContentValues(); contentValues.put(HistoryDbColumn.CONTENT,history.getTime()); long num = sqliteDatabase.update(table_name,contentValues,new String[]{String.valueOf(ID)}); return num; } /** * 根据ID查询一条数据 * * @param ID * @return */ public History qurey(int ID) { History history = null; Cursor cursor = sqliteDatabase.query(table_name,new String[]{String.valueOf(ID)},null); if (cursor != null) { if (cursor.movetoNext()) { history = new History(); history.setID(cursor.getInt(cursor.getColumnIndex(HistoryDbColumn.ID))); history.setContent(cursor.getString(cursor.getColumnIndex(HistoryDbColumn.CONTENT))); history.setTime(cursor.getString(cursor.getColumnIndex(HistoryDbColumn.TIME))); } } return history; } /** * 根据ID查询一条数据 * 倒序 * * @return */ public List<History> queryAll() { List<History> historys = new ArrayList<>(); Cursor cursor = sqliteDatabase.query(table_name,HistoryDbColumn.TIME + " desc"); if (cursor != null) { while (cursor.movetoNext()) { History history = new History(); history.setID(cursor.getInt(cursor.getColumnIndex(HistoryDbColumn.ID))); history.setContent(cursor.getString(cursor.getColumnIndex(HistoryDbColumn.CONTENT))); history.setTime(cursor.getString(cursor.getColumnIndex(HistoryDbColumn.TIME))); historys.add(history); } } return historys; } /** * 根据内容查询一条数据 * * @return */ public History queryByContent(String content) { History history = null; Cursor cursor = sqliteDatabase.query(table_name,HistoryDbColumn.CONTENT + "=?",new String[]{content},null); if (cursor != null) { if (cursor.movetoNext()) { history = new History(); history.setID(cursor.getInt(cursor.getColumnIndex(HistoryDbColumn.ID))); history.setContent(cursor.getString(cursor.getColumnIndex(HistoryDbColumn.CONTENT))); history.setTime(cursor.getString(cursor.getColumnIndex(HistoryDbColumn.TIME))); } } return history; } } 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 三.搜索对话框的布局文件 <?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:minHeight="250dp" androID:orIEntation="vertical"> <cn.bluemobi.dylan.sqlite.SearchVIEw androID:ID="@+ID/sv" androID:padding="10dp" androID:background="@color/colorPrimaryDark" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> </cn.bluemobi.dylan.sqlite.SearchVIEw> <ListVIEw androID:ID="@+ID/lv" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"></ListVIEw> <TextVIEw androID:ID="@+ID/tv" androID:layout_gravity="center" androID:gravity="center" androID:layout_weight="1" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:text="暂无搜索记录" /> </linearLayout> 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 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 四.编写功能代码 package cn.bluemobi.dylan.sqlite; import androID.app.Dialog; import androID.os.Bundle; import androID.support.annotation.Nullable; import androID.support.v7.app.AppCompatActivity; import androID.util.Log; import androID.vIEw.Gravity; import androID.vIEw.VIEw; import androID.vIEw.WindowManager; import androID.Widget.button; import androID.Widget.EditText; import androID.Widget.ListVIEw; import androID.Widget.TextVIEw; import androID.Widget.Toast; import java.util.Date; import java.util.List; import cn.bluemobi.dylan.sqlite.adapter.CommonAdapter; import cn.bluemobi.dylan.sqlite.adapter.CommonVIEwHolder; /** * sqlite应用案例实现搜索记录 * Created by administrator on 2016-11-20. */ public class SearchActivity extends AppCompatActivity implements VIEw.OnClickListener { private EditText et; private ListVIEw lv; private TextVIEw tv; private Dialog dialog; private SearchVIEw sv; private button bt; private List<History> historIEs; private CommonAdapter<History> commonAdapter; private final int MAX_ITME = 5; private voID assignVIEws() { et = (EditText) findVIEwByID(R.ID.et); } @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionbar().setTitle("sqlite应用案例实现搜索记录"); setContentVIEw(R.layout.ac_search); assignVIEws(); intIDialog(); addListener(); initData(); } /** * 添加按钮监听 */ private voID addListener() { et.setonClickListener(this); } /*** * 初始化搜索对话框 */ private voID intIDialog() { dialog = new Dialog(this,R.style.Dialog_FullScreen); dialog.setContentVIEw(R.layout.dialog_search); dialog.getwindow().setGravity(Gravity.top); dialog.setCanceledOntouchOutsIDe(true); dialog.setCancelable(true); WindowManager.LayoutParams lp = dialog.getwindow().getAttributes(); lp.wIDth = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; dialog.getwindow().setAttributes(lp); lv = (ListVIEw) dialog.findVIEwByID(R.ID.lv); tv = (TextVIEw) dialog.findVIEwByID(R.ID.tv); sv = (SearchVIEw) dialog.findVIEwByID(R.ID.sv); bt = (button) dialog.findVIEwByID(R.ID.bt); bt.setonClickListener(this); lv.setEmptyVIEw(tv); } /** * 初始化数据 */ private voID initData() { commonAdapter = new CommonAdapter<History>(this,historIEs,R.layout.item_for_search) { @OverrIDe protected voID convertVIEw(CommonVIEwHolder commonVIEwHolder,History history) { TextVIEw tv = commonVIEwHolder.get(R.ID.textVIEw); tv.setText(history.getContent()); } }; lv.setAdapter(commonAdapter); notifyAdapter(); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.et: if (!dialog.isShowing()) { dialog.show(); } break; case R.ID.bt: addHistory(); break; } } [url]http://mp.weixin.qq.com/s?__biz=MzIxNjY4MTgyNQ==&tempkey=5lh0a7qW[url]http://mp.weixin.qq.com/s/QN4ZGOuGcDeOuFWbC6aeFQ[/url][/url] [/url][/url]http://mp.weixin.qq.com/s?__biz=MzIxNjY4MTgyNQ==&tempkey=5lh0a7qWmqqNMpgubVo6zMpG%2Fpe8Jw7YRKEMqE4Fl7NbuIq%2BwOfYSQEB44oCwv0Cj6HxXRNmSOoNDrD9%2F8WoPnkLMW1%2BWcz9HtZ9ytp136nAgrF7V%2ByzG0jupCghFx5OUZWtmyyhsey0cEzFk5WsBQ%3D%3D&chksm=17841d0f20f394193a92974082356032a0f27f337c60ed8cf140a31dd22b005ac0b505b9cf97#rd[/url][/url] [/url][/url]http://mp.weixin.qq.com/s?__biz=MzIxNjY4MTgyNQ==&tempkey=5lh0a7qWhttp://mp.weixin.qq.com/s/QN4ZGOuGcDeOuFWbC6aeFQ[/url][/url] [/url][/url]http://mp.weixin.qq.com/s?__biz=MzIxNjY4MTgyNQ==&mID=2247483651&IDx=1&sn=0ab6116dbc93d0c0e51c227e488d0234&chksm=97841d0da0f3941b216e255af8c4b247c8b378750dac9ec2663dd0427057070f0297e218bc52#rd[/url][/url] [/url][/url]http://mp.weixin.qq.com/s?__biz=MzIxNjY4MTgyNQ==&tempkey=5lh0a7qWmqqNMpgubVo6zMpG%2Fpe8Jw7YRKEMqE4Fl7NbuIq%2BwOfYSQEB44oCwv0Cj6HxXRNmSOoNDrD9%2F8WoPnkLMW1%2BWcz9HtZ9ytp136nAgrF7V%2ByzG0jupCghFx5OUZWtmyyhsey0cEzFk5WsBQ%3D%3D&chksm=17841d0f20f394193a92974082356032a0f27f337c60ed8cf140a31dd22b005ac0b505b9cf97#rd[/url][/url] /** * 点击搜索按钮新增一条记录 */ private voID addHistory() { String inputText = sv.getinputText(); if (inputText.isEmpty()) { Toast.makeText(this,"请输入内容进行搜索",Toast.LENGTH_SHORT).show(); return; } /**1.先判断数据库当中有没有这条历史记录,如果有则修改其搜索的时间即可*/ History history = DBManager.getDBManager().queryByContent(inputText); if (history != null) { history.setTime(new Date().toString()); DBManager.getDBManager().update(history,history.getID()); } else { /**2.判断搜索记录是否达到限值,达到极限则删除一条数据**/ if (historIEs != null && historIEs.size() == MAX_ITME) { DBManager.getDBManager().delete(historIEs.get(historIEs.size() - 1).getID()); } /**3.插入一条数据**/ history = new History(); history.setContent(sv.getinputText()); history.setTime(new Date().toString()); long num = DBManager.getDBManager().insert(history); if (num != -1) { Log.d(Contacts.TAG,"插入成功"); } else { Log.d(Contacts.TAG,"插入失败"); } } notifyAdapter(); } /** * 更新数据库当中的数据 */ private voID notifyAdapter() { historIEs = DBManager.getDBManager().queryAll(); commonAdapter.notifyDataSetChanged(historIEs); } 总结

以上是内存溢出为你收集整理的SQLite数据库轻量级ORM *** 作数据库工具类全部内容,希望文章能够帮你解决SQLite数据库轻量级ORM *** 作数据库工具类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1164712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存