Android數(shù)據(jù)庫(kù)操作工具類分享
本文實(shí)例為大家分享了Android數(shù)據(jù)庫(kù)操作工具類的具體代碼,供大家參考,具體內(nèi)容如下
HistoryDAO
public class HistoryDAO {
private DBConnection dbc = null;
private SQLiteDatabase db = null;
private Context context;
//數(shù)據(jù)庫(kù)上下文
public HistoryDAO(Context context) {
this.context = context;
}
//打開(kāi)數(shù)據(jù)庫(kù)
public HistoryDAO open() {
dbc = new DBConnection(context);
db = dbc.getWritableDatabase();
return this;
}
//關(guān)閉數(shù)據(jù)庫(kù)
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();
}
// 增加 工具類的最后五個(gè)專用
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ù)庫(kù)查詢表字段
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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(fā)使用json實(shí)現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能示例
- Android App端與PHP Web端的簡(jiǎn)單數(shù)據(jù)交互實(shí)現(xiàn)示例
- Android登錄注冊(cè)功能 數(shù)據(jù)庫(kù)SQLite驗(yàn)證
- Android開(kāi)發(fā)中數(shù)據(jù)庫(kù)升級(jí)且表添加新列的方法
- Android SQLite數(shù)據(jù)庫(kù)版本升級(jí)的管理實(shí)現(xiàn)
- Android數(shù)據(jù)傳輸中的參數(shù)加密代碼示例
相關(guān)文章
android中intent傳遞list或者對(duì)象的方法
這篇文章主要介紹了android中intent傳遞list或者對(duì)象的方法,分析羅列了常用的幾種方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
Android實(shí)現(xiàn)圖片異步加載及本地緩存
這篇文章主要介紹了Android實(shí)現(xiàn)圖片異步加載及本地緩存的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android編程簡(jiǎn)單設(shè)置ListView分割線的方法
這篇文章主要介紹了Android編程簡(jiǎn)單設(shè)置ListView分割線的方法,涉及Android布局簡(jiǎn)單操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
android獲取照片的快照 思路及實(shí)現(xiàn)方法
android獲取照片的快照 思路及實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-06-06
Android提高之多方向抽屜實(shí)現(xiàn)方法
這篇文章主要介紹了Android的多方向抽屜實(shí)現(xiàn)方法,有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
解決Android調(diào)用系統(tǒng)分享給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式的問(wèn)題
這篇文章主要介紹了解決Android調(diào)用系統(tǒng)分享給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁(yè)效果
這篇文章主要介紹了Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁(yè)效果,本文通過(guò)實(shí)例代碼結(jié)合文字說(shuō)明的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-03-03
Android自定義控件實(shí)現(xiàn)邊緣凹凸的卡劵效果
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)邊緣凹凸的卡劵效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07

