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

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

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

本文实例讲述了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,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);      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;  }}

测试代码:

@OverrIDeprotected 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,selectionArgs);    contentTextVIEw.setText("邮编:" + city.getAreaCode() + "拼音:" + city.getPinyin() + "电话区号" + city.getPhoneCode() + "cityID:" + city.getCityID());}

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

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

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

总结

以上是内存溢出为你收集整理的Android开发实现读取assets目录下db文件的方法示例全部内容,希望文章能够帮你解决Android开发实现读取assets目录下db文件的方法示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143998.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存