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

Android开发实现读取assets目录下db文件的方法示例

程序员文章站 2023-11-30 22:04:58
本文实例讲述了android开发实现读取assets目录下db文件的方法。分享给大家供大家参考,具体如下: 最近准备打算写一个关于天气预报的app,偶然的机会在一大神的博...

本文实例讲述了android开发实现读取assets目录下db文件的方法。分享给大家供大家参考,具体如下:

最近准备打算写一个关于天气预报的app,偶然的机会在一大神的博客上看到了一个获取天气的api,获取天气是通过城市的cityid,项目中准备通过读取weather_city.db数据库来查询cityid,这篇文章写怎么读取assets目录下的db文件,其实方法也挺简单的就是把assets目录下的db文件复制一份到”/data/data/” + packname + “/”目录下而已。

public class dbmanager {
  private string db_name = "weather_city.db";
  private context mcontext;
  public dbmanager(context mcontext) {
    this.mcontext = mcontext;
  }
  //把assets目录下的db文件复制到dbpath下
  public sqlitedatabase dbmanager(string packname) {
    string dbpath = "/data/data/" + packname
        + "/databases/" + db_name;
    if (!new file(dbpath).exists()) {
      try {
        fileoutputstream out = new fileoutputstream(dbpath);
        inputstream in = mcontext.getassets().open("weather_city.db");
        byte[] buffer = new byte[1024];
        int readbytes = 0;
        while ((readbytes = in.read(buffer)) != -1)
          out.write(buffer, 0, readbytes);
        in.close();
        out.close();
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
    return sqlitedatabase.openorcreatedatabase(dbpath, null);
  }
  //查询
  public city query(sqlitedatabase sqlitedb, string[] columns, string selection, string[] selectionargs) {
    city city = null;
    try {
      string table = "city";
      cursor cursor = sqlitedb.query(table, columns, selection, selectionargs, null, null, null);
      if (cursor.movetofirst()) {
        string parentcity = cursor.getstring(cursor
            .getcolumnindex("parent"));
        string phonecode = cursor.getstring(cursor.getcolumnindex("phone_code"));
        string name = cursor.getstring(cursor.getcolumnindex("name"));
        string pinyin = cursor.getstring(cursor.getcolumnindex("pinyin"));
        string cityid = cursor.getstring(cursor.getcolumnindex("posid"));
        string areacode = cursor.getstring(cursor.getcolumnindex("area_code"));
        city = new city(parentcity, name, pinyin, phonecode, cityid, areacode);
        cursor.movetonext();
        cursor.close();
      }
    } catch (exception e) {
      e.printstacktrace();
    }
    return city;
  }
}

为了方便数据的使用,我们建一个city类,对应city表中的字段,如下:

public class city {
  private string parentcity;
  private string childcity;
  private string pinyin;
  private string phonecode;
  private string cityid;
  private string areacode;
  public city(string parentcity, string childcity, string pinyin, string phonecode, string cityid, string areacode) {
    this.parentcity = parentcity;
    this.childcity = childcity;
    this.pinyin = pinyin;
    this.phonecode = phonecode;
    this.cityid = cityid;
    this.areacode = areacode;
  }
  public string getparentcity() {
    return parentcity;
  }
  public void setparentcity(string parentcity) {
    this.parentcity = parentcity;
  }
  public string getareacode() {
    return areacode;
  }
  public void setareacode(string areacode) {
    this.areacode = areacode;
  }
  public string getcityid() {
    return cityid;
  }
  public void setcityid(string cityid) {
    this.cityid = cityid;
  }
  public string getphonecode() {
    return phonecode;
  }
  public void setphonecode(string phonecode) {
    this.phonecode = phonecode;
  }
  public string getpinyin() {
    return pinyin;
  }
  public void setpinyin(string pinyin) {
    this.pinyin = pinyin;
  }
  public string getchildcity() {
    return childcity;
  }
  public void setchildcity(string childcity) {
    this.childcity = childcity;
  }
}

测试代码:

@override
protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    contenttextview = (textview) findviewbyid(r.id.content);
    dbmanager = new dbmanager(this);
    sqlitedatabase = dbmanager.initdbmanager(getpackagename());
    string[] columns = new string[]{"parent", "name", "posid", "pinyin", "phone_code", "area_code"};
    string selection = "parent=?" + "and" + " name=?";
    string[] selectionargs = new string[]{"北京", "丰台"};
    city city = dbmanager.query(sqlitedatabase, columns, selection, selectionargs);
    contenttextview.settext("邮编:" + city.getareacode() + "拼音:" + city.getpinyin() + "电话区号" + city.getphonecode() + "cityid:" + city.getcityid());
}

Android开发实现读取assets目录下db文件的方法示例

读取的数据与表中的数据一致

Android开发实现读取assets目录下db文件的方法示例

更多关于android相关内容感兴趣的读者可查看本站专题:《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android布局layout技巧总结》、《android开发入门与进阶教程》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。