溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android開發如何讀取assets目錄下db文件

發布時間:2021-07-10 16:55:01 來源:億速云 閱讀:350 作者:小新 欄目:移動開發

這篇文章主要為大家展示了“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, 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開發如何讀取assets目錄下db文件”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女