Android數(shù)據(jù)庫操作工具類分享
更新時間:2017年10月09日 15:12:50 作者:JustingWang_1
這篇文章主要為大家詳細介紹了Android數(shù)據(jù)庫操作工具類的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android數(shù)據(jù)庫操作工具類的具體代碼,供大家參考,具體內(nèi)容如下
HistoryDAO
public class HistoryDAO { private DBConnection dbc = null; private SQLiteDatabase db = null; private Context context; //數(shù)據(jù)庫上下文 public HistoryDAO(Context context) { this.context = context; } //打開數(shù)據(jù)庫 public HistoryDAO open() { dbc = new DBConnection(context); db = dbc.getWritableDatabase(); return this; } //關(guān)閉數(shù)據(jù)庫 public void closeAll() { db.close(); dbc.close(); } // // 增加 // public void add(Search_HistoryData data, String type) { // open(); // ContentValues values = new ContentValues(); // values.put("content", data.getContent()); // values.put("type", data.getType()); // db.insert("history", null, values); // closeAll(); // } // 增加 public void add(Search_HistoryData data, String tableName) { open(); ContentValues values = new ContentValues(); values.put("content", data.getContent()); db.insert(tableName, null, values); closeAll(); } // 增加 工具類的最后五個專用 public void addLawTool(Search_HistoryData data, String tableName) { open(); ContentValues values = new ContentValues(); values.put("content", data.getContent()); values.put("_id", data.getId()); db.insert(tableName, null, values); closeAll(); } // 全查詢 public List getAll(String TableName) { open(); List ar = new ArrayList(); Cursor c = db.rawQuery("select * from " + TableName, null); while (c.moveToNext()) { Map map = new HashMap(); map.put("_id", c.getInt(c.getColumnIndex("_id"))); map.put("content", c.getString(c.getColumnIndex("content"))); ar.add(map); } closeAll(); return ar; } // 刪除 根據(jù)id刪除 public void delete(String tableName, int uid) { open(); db.delete("history", "uid=" + uid, null); closeAll(); } //清空表中所有數(shù)據(jù) public void delete(String tableName) { open(); db.delete(tableName, null, null); closeAll(); } //判斷是否存在 public boolean searchResult(String tableName, String key) { open(); Boolean booleans = db.rawQuery("select * from " + tableName + " where content = ?", new String[]{key}).moveToNext(); closeAll(); return booleans; } //根據(jù)庫查詢表字段 public boolean searchResultToType(String content, String type) { open(); Boolean booleans = db.rawQuery("select * from history where content = ? and type = ?", new String[]{content, type}).moveToNext(); closeAll(); return booleans; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 6.0以上權(quán)限拒絕打開權(quán)限設(shè)置界面的解決方法
今天小編就為大家分享一篇Android 6.0以上權(quán)限拒絕打開權(quán)限設(shè)置界面的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android 中對于圖片的內(nèi)存優(yōu)化方法
Android 中對于圖片的內(nèi)存優(yōu)化方法,需要的朋友可以參考一下2013-03-03Android實現(xiàn)頁面翻轉(zhuǎn)和自動翻轉(zhuǎn)功能
這篇文章主要介紹了Android中簡單實現(xiàn)頁面翻轉(zhuǎn)和自動翻轉(zhuǎn)的功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10

Android Uri和文件路徑互相轉(zhuǎn)換的實例代碼
在項目中需要用到將Uri轉(zhuǎn)換為絕對路徑,下面小編把Android Uri和文件路徑互相轉(zhuǎn)換的實例代碼分享到腳本之家平臺,需要的的朋友參考下吧
2017-07-07