没有合适的资源?快使用搜索试试~ 我知道了~
首页Android开发实现读取assets目录下db文件的方法示例
资源详情
资源评论
资源推荐

Android开发实现读取开发实现读取assets目录下目录下db文件的方法示例文件的方法示例
主要介绍了Android开发实现读取assets目录下db文件的方法,结合实例形式分析了Android针对assets目录下
SQLite数据库文件的相关操作技巧,需要的朋友可以参考下
本文实例讲述了Android开发实现读取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;


















weixin_38528086
- 粉丝: 2
- 资源: 921
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
最新资源
- Xilinx SRIO详解.pptx
- Informatica PowerCenter 10.2 for Centos7.6安装配置说明.pdf
- 现代无线系统射频电路实用设计卷II 英文版.pdf
- 电子产品可靠性设计 自己讲课用的PPT,包括设计方案的可靠性选择,元器件的选择与使用,降额设计,热设计,余度设计,参数优化设计 和 失效分析等
- MPC5744P-DEV-KIT-REVE-QSG.pdf
- 通信原理课程设计报告(ASK FSK PSK Matlab仿真--数字调制技术的仿真实现及性能研究)
- ORIGIN7.0使用说明
- 在VMware Player 3.1.3下安装Redhat Linux详尽步骤
- python学生信息管理系统实现代码
- 西门子MES手册 13 OpcenterEXCR_PortalStudio1_81RB1.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论0