溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android數(shù)據(jù)庫(kù)操作工具類分享

發(fā)布時(shí)間:2020-09-17 21:51:39 來(lái)源:腳本之家 閱讀:157 作者:JustingWang_1 欄目:移動(dòng)開發(fā)

本文實(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;
  }
  //打開數(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í)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI