bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Android開發(fā)如何讀取assets目錄下db文件-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“Android開發(fā)如何讀取assets目錄下db文件”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android開發(fā)如何讀取assets目錄下db文件”這篇文章吧。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、成都小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了崖州免費(fèi)建站歡迎大家使用!

具體如下:

最近準(zhǔn)備打算寫一個(gè)關(guān)于天氣預(yù)報(bào)的app,偶然的機(jī)會(huì)在一大神的博客上看到了一個(gè)獲取天氣的api,獲取天氣是通過城市的cityID,項(xiàng)目中準(zhǔn)備通過讀取weather_city.db數(shù)據(jù)庫來查詢cityID,這篇文章寫怎么讀取assets目錄下的db文件,其實(shí)方法也挺簡(jiǎn)單的就是把a(bǔ)ssets目錄下的db文件復(fù)制一份到”/data/data/” + packName + “/”目錄下而已。

public class DBManager {
  private String DB_NAME = "weather_city.db";
  private Context mContext;
  public DBManager(Context mContext) {
    this.mContext = mContext;
  }
  //把a(bǔ)ssets目錄下的db文件復(fù)制到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;
  }
}

為了方便數(shù)據(jù)的使用,我們建一個(gè)City類,對(duì)應(yīng)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;
  }
}

測(cè)試代碼:

@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[]{"北京", "豐臺(tái)"};
    City city = dbManager.query(sqLiteDatabase, columns, selection, selectionArgs);
    contentTextView.setText("郵編:" + city.getAreaCode() + "拼音:" + city.getPinyin() + "電話區(qū)號(hào)" + city.getPhoneCode() + "cityID:" + city.getCityID());
}

Android開發(fā)如何讀取assets目錄下db文件

讀取的數(shù)據(jù)與表中的數(shù)據(jù)一致

Android開發(fā)如何讀取assets目錄下db文件

以上是“Android開發(fā)如何讀取assets目錄下db文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文標(biāo)題:Android開發(fā)如何讀取assets目錄下db文件-創(chuàng)新互聯(lián)
鏈接URL:http://vcdvsql.cn/article36/depppg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司靜態(tài)網(wǎng)站搜索引擎優(yōu)化企業(yè)網(wǎng)站制作用戶體驗(yàn)響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)