Android開發(fā)實現(xiàn)讀取assets目錄下db文件的方法示例
本文實例講述了Android開發(fā)實現(xiàn)讀取assets目錄下db文件的方法。分享給大家供大家參考,具體如下:
最近準(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ù)一致

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android中ListView分頁加載數(shù)據(jù)功能實現(xiàn)
本篇文章主要介紹了Android中ListView分頁加載數(shù)據(jù)功能實現(xiàn),具有一定的參考價值,有需要的可以了解一下。2016-11-11
Android App中的多個LinearLayout嵌套布局實例解析
這篇文章主要介紹了Android App中的多個LinearLayout嵌套布局實例,利用線性布局來排列按鈕是安卓應(yīng)用布局中的常用做法,需要的朋友可以參考下2016-04-04
5個Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法
本文主要介紹了5個Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
Android開發(fā)中ImageView的scaletype屬性用法分析
這篇文章主要介紹了Android開發(fā)中ImageView的scaletype屬性用法,分析了scaletype屬性參數(shù)的常見功能并結(jié)合實例形式給出了具體的使用方法,需要的朋友可以參考下2016-08-08
Android搭建本地Tomcat服務(wù)器及相關(guān)配置
這篇文章主要介紹了Android搭建本地Tomcat服務(wù)器及相關(guān)配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
android系統(tǒng)在靜音模式下關(guān)閉camera拍照聲音的方法
本文為大家詳細介紹下android系統(tǒng)如何在靜音模式下關(guān)閉camera拍照聲音,具體的實現(xiàn)方法如下,感興趣的朋友可以參考下哈2013-07-07

