欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android数据库操作工具类分享

程序员文章站 2024-02-11 21:38:16
本文实例为大家分享了android数据库操作工具类的具体代码,供大家参考,具体内容如下 historydao public class historydao...

本文实例为大家分享了android数据库操作工具类的具体代码,供大家参考,具体内容如下

historydao

public class historydao {
  private dbconnection dbc = null;
  private sqlitedatabase db = null;
  private context context;

  //数据库上下文
  public historydao(context context) {
    this.context = context;
  }
  //打开数据库
  public historydao open() {
    dbc = new dbconnection(context);
    db = dbc.getwritabledatabase();
    return this;
  }

  //关闭数据库
  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;
  }

  // 删除 根据id删除
  public void delete(string tablename, int uid) {
    open();
    db.delete("history", "uid=" + uid, null);
    closeall();
  }

  //清空表中所有数据
  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;
  }

  //根据库查询表字段
  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;
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。