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

首頁(yè) > 系統(tǒng) > Android > 正文

Android開(kāi)發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法示例

2019-12-12 01:50:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法。分享給大家供大家參考,具體如下:

最近準(zhǔn)備打算寫(xiě)一個(gè)關(guān)于天氣預(yù)報(bào)的app,偶然的機(jī)會(huì)在一大神的博客上看到了一個(gè)獲取天氣的api,獲取天氣是通過(guò)城市的cityID,項(xiàng)目中準(zhǔn)備通過(guò)讀取weather_city.db數(shù)據(jù)庫(kù)來(lái)查詢(xún)cityID,這篇文章寫(xiě)怎么讀取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);  }  //查詢(xún)  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類(lèi),對(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è)試代碼:

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

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

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黔西县| 平乡县| 乐陵市| 开封县| 泸溪县| 永靖县| 金门县| 连城县| 通渭县| 双城市| 尤溪县| 津南区| 桃源县| 喜德县| 普兰店市| 成武县| 娄烦县| 永福县| 南汇区| 宾川县| 黄平县| 互助| 嵊州市| 德保县| 榕江县| 荆门市| 浮梁县| 乌鲁木齐县| 策勒县| 高陵县| 绥宁县| 大洼县| 合水县| 瑞金市| 通许县| 孟津县| 临安市| 大丰市| 娄烦县| 常山县| 巨野县|