发表于: 2018-10-19 22:56:37
0 790
一、今天完成的事情
本来计划今天实现用户设置地址这个功能,然后完成任务四,但因为对SQLite不熟悉,中间各种采坑,还是没能完成。
思路是布局中添加三个NumberPicker,分别对应省、市、区,三个NumberPicker形成联动,省市区的数据来自于本地数据库。数据库放在assets目录下,这样就不会被编译,但也不能直接使用。使用时先把数据库复制到android默认的数据库存储路径data/data/database下。
public static void copyAssetsToDB(Context context,String fileName) throws IOException {
//地区数据库路径
String destPath = context.getDatabasePath(fileName).getPath();
Log.d(TAG, "copyAssetsToDB: 数据库路径:" + destPath);
File file = new File(destPath);
if (!file.exists()) {
file.mkdirs();
} else {
return;
}
InputStream is = context.getAssets().open(fileName);
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(destPath + File.separator + fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] data = new byte[2 * 1024];
int len;
while((len = bis.read(data)) != -1) {
bos.write(data, 0, len);
}
bos.flush();
bis.close();
bos.close();
}
//复制assets下的mydb.db文件到应用的databases目录下
try {
Log.d(TAG, "onCreateView: 开始复制");
MyUtils.copyAssetsToDB(this, "mydb.db");
Log.d(TAG, "onCreateView: 复制完成");
} catch (IOException e) {
e.printStackTrace();
}
到这一步一直没出问题,mydb.db已经成功复制到databases目录中了,然后从数据库中查数据时,问题出现了
public class AddressManager {
private static final String TABLE_NAME = "ssq"; //表名
private static final String PROVINCE = "province"; //省
private static final String CITY = "city"; //市
private static final String AREA = "area"; //区/县
private SQLiteDatabase database;
/**
* @param dbPath 数据库中“省市区”表的路径
*/
public AddressManager(String dbPath) {
database = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
}
/**
* 查询所有省
*/
public List<String> getProvinces() {
String sql = "select distinct " + PROVINCE + " from " + TABLE_NAME;
Cursor cursor = database.rawQuery(sql, null);
List<String> provinceList = new ArrayList<>();
if (cursor != null) {
while (cursor.moveToNext()) {
String province = cursor.getString(cursor.getColumnIndex("province"));
provinceList.add(province);
}
cursor.close();
}
return provinceList;
}
/**
* 查询指定省份或直辖市的所有市的集合
*/
public List<String> getCities(String province) {
String sql = "select distinct " + CITY + " from " + TABLE_NAME + " where " + PROVINCE + " = ?";
Cursor cursor = database.rawQuery(sql, new String[]{province});
List<String> addressList = new ArrayList<>();
while (cursor.moveToNext()) {
String city = cursor.getString(cursor.getColumnIndex(CITY));
Log.i("tag", "city----->" + city);
addressList.add(city);
}
cursor.close();
return addressList;
}
/**
* 查询指定市的所有区的列表的List集合
*/
public List<String> getAreas(String city) {
//获取指定市的所有区的列表的sql语句
String sql = "select distinct " + AREA + " from " + TABLE_NAME + " where " + CITY + " = ?";
Cursor cursor = database.rawQuery(sql, new String[]{city});
List<String> addressList = new ArrayList<>();
while (cursor.moveToNext()) {
//获取区的名称
String area = cursor.getString(cursor.getColumnIndex(AREA));
Log.i("tag", "area----->" + area);
//把所有的区添加到List集合
addressList.add(area);
}
cursor.close();
return addressList;
}
}
SD卡权限已经给了。
数据也没有问题。
二、明天计划的事情
提交任务四
三、遇到的问题
无法打开database中的数据库文件
SQLiteDatabase: Failed to open database '/data/user/0/com.example.forrestsu.logintest/databases/mydb.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
四、收获
CircleImageView
从相册选取图片
评论