Android開發(fā)實現讀取assets目錄下db文件的方法示例
本文實例講述了Android開發(fā)實現讀取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() + "電話區(qū)號" + city.getPhoneCode() + "cityID:" + city.getCityID()); }
讀取的數據與表中的數據一致
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android操作SQLite數據庫技巧總結》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android布局layout技巧總結》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android App中的多個LinearLayout嵌套布局實例解析
這篇文章主要介紹了Android App中的多個LinearLayout嵌套布局實例,利用線性布局來排列按鈕是安卓應用布局中的常用做法,需要的朋友可以參考下2016-04-045個Android開發(fā)中比較常見的內存泄漏問題及解決辦法
本文主要介紹了5個Android開發(fā)中比較常見的內存泄漏問題及解決辦法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02Android開發(fā)中ImageView的scaletype屬性用法分析
這篇文章主要介紹了Android開發(fā)中ImageView的scaletype屬性用法,分析了scaletype屬性參數的常見功能并結合實例形式給出了具體的使用方法,需要的朋友可以參考下2016-08-08android系統(tǒng)在靜音模式下關閉camera拍照聲音的方法
本文為大家詳細介紹下android系統(tǒng)如何在靜音模式下關閉camera拍照聲音,具體的實現方法如下,感興趣的朋友可以參考下哈2013-07-07