Android開發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法示例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法。分享給大家供大家參考,具體如下:
最近準(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í)方法也挺簡單的就是把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;
}
}
測試代碼:
@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());
}

讀取的數(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ā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編寫文件瀏覽器簡單實(shí)現(xiàn)
- Android中調(diào)用系統(tǒng)的文件瀏覽器及自制簡單的文件瀏覽器
- 微信或手機(jī)瀏覽器在線顯示office文件(已測試ios、android)
- 讀寫Android中assets目錄下的文件的方法詳解
- Android如何遍歷特定目錄下所有文件
- Android遍歷所有文件夾和子目錄搜索文件
- 讀取android根目錄下的文件或文件夾實(shí)例
- Android 將文件下載到指定目錄的實(shí)現(xiàn)代碼
- Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法
- Android編程實(shí)現(xiàn)簡單文件瀏覽器功能
相關(guān)文章
Android中ListView分頁加載數(shù)據(jù)功能實(shí)現(xiàn)
本篇文章主要介紹了Android中ListView分頁加載數(shù)據(jù)功能實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
Android App中的多個(gè)LinearLayout嵌套布局實(shí)例解析
這篇文章主要介紹了Android App中的多個(gè)LinearLayout嵌套布局實(shí)例,利用線性布局來排列按鈕是安卓應(yīng)用布局中的常用做法,需要的朋友可以參考下2016-04-04
5個(gè)Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法
本文主要介紹了5個(gè)Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
Android開發(fā)中ImageView的scaletype屬性用法分析
這篇文章主要介紹了Android開發(fā)中ImageView的scaletype屬性用法,分析了scaletype屬性參數(shù)的常見功能并結(jié)合實(shí)例形式給出了具體的使用方法,需要的朋友可以參考下2016-08-08
實(shí)現(xiàn)qq中按返回鍵返回桌面不退出程序的實(shí)例
下面小編就為大家?guī)硪黄獙?shí)現(xiàn)qq中按返回鍵返回桌面不退出程序的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android搭建本地Tomcat服務(wù)器及相關(guān)配置
這篇文章主要介紹了Android搭建本地Tomcat服務(wù)器及相關(guān)配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
android系統(tǒng)在靜音模式下關(guān)閉camera拍照聲音的方法
本文為大家詳細(xì)介紹下android系統(tǒng)如何在靜音模式下關(guān)閉camera拍照聲音,具體的實(shí)現(xiàn)方法如下,感興趣的朋友可以參考下哈2013-07-07

