這篇文章主要為大家展示了“Android開發(fā)如何讀取assets目錄下db文件”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android開發(fā)如何讀取assets目錄下db文件”這篇文章吧。
目前創(chuàng)新互聯(lián)公司已為超過千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計、建華網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
具體如下:
最近準(zhǔn)備打算寫一個關(guān)于天氣預(yù)報的app,偶然的機會在一大神的博客上看到了一個獲取天氣的api,獲取天氣是通過城市的cityID,項目中準(zhǔn)備通過讀取weather_city.db數(shù)據(jù)庫來查詢cityID,這篇文章寫怎么讀取assets目錄下的db文件,其實方法也挺簡單的就是把assets目錄下的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; } //把assets目錄下的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ù)的使用,我們建一個City類,對應(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; } }
測試代碼:
@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() + "電話區(qū)號" + city.getPhoneCode() + "cityID:" + city.getCityID()); }
讀取的數(shù)據(jù)與表中的數(shù)據(jù)一致
以上是“Android開發(fā)如何讀取assets目錄下db文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!