国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發實現讀取assets目錄下db文件的方法示例

2019-10-22 18:25:13
字體:
來源:轉載
供稿:網友

本文實例講述了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;  }}

測試代碼:

@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, columns, selection, selectionArgs);    contentTextView.setText("郵編:" + city.getAreaCode() + "拼音:" + city.getPinyin() + "電話區號" + city.getPhoneCode() + "cityID:" + city.getCityID());}

Android,讀取,assets目錄,db文件

讀取的數據與表中的數據一致

Android,讀取,assets目錄,db文件

 

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和顺县| 白银市| 呼图壁县| 庄河市| 鹤山市| 新和县| 南开区| 云阳县| 修武县| 兰西县| 丹凤县| 朝阳区| 淮滨县| 阿荣旗| 乌苏市| 神农架林区| 广宁县| 彭州市| 南阳市| 祁东县| 木里| 文化| 连平县| 历史| 武安市| 普安县| 平潭县| 五常市| 临邑县| 岚皋县| 湛江市| 万源市| 无为县| 灵璧县| 大同市| 麦盖提县| 佛学| 合阳县| 济阳县| 遂昌县| 长宁区|