IT修真院·坑乎 | Java(后端),CSS(前端),JavaScript(前端),PM(产品经理),UI(交互设计),QA(测试),Android(安卓APP),iOS(苹果APP),OP(运维),Python(后端)日常在线问答学习平台
咨询电话 : 010-59478634
切换导航
首页
我的提问
我的回答
我的点赞
消息通知
个人主页
×
提示
尚未登陆,前往官网登陆?
×
提示
尚未登陆,前往官网登陆?
CSS
JS
Android
IOS
JAVA
OP
PM
UI
QA
求职辅导
Python
运营
动画师
AIGC
CSS
JS
Android
IOS
JAVA
OP
PM
UI
QA
求职辅导
Python
运营
动画师
AIGC
所有任务
任务1
任务2
任务3
任务4
任务5
任务6
任务7
任务8
任务9
任务10
任务11
任务12
任务13
任务14
任务15
任务16
任务17
全部
问题
已收录
问题
未收录
问题
没有找到答案,去提问>>>
该问题已被官方收录
无法打开database中的数据库
0
1
class="cursor-pointer"> 详细描述
读取database中的mydb.db这个数据库,读取过程中app崩溃了 网上的方法都是说授予SD卡读取权限,但我已经给了读写权限了
class="cursor-pointer"> 截图
class="cursor-pointer"> 代码
public class SetAddressFragment extends Fragment { private static final String TAG = "SetAddressFragment"; private static final String DB_NAME = "mydb.db"; private TextView confirmTV, cancelTV; private NumberPicker provinceNP, cityNP, areaNP; private AddressManager manager; private SendMessageAddress sendMessageAddress; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle saveInstanceState) { View square = inflater.inflate(R.layout.fragment_setaddress, container, false); manager = new AddressManager(getContext().getDatabasePath(DB_NAME).getPath()); initView(square); initProvince(); return square; } ...... } ##################################################################################################################### 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); } /** * 查询所有省份 * * @return 所有的省份名称 */ public List
getProvinces() { String sql = "select distinct " + PROVINCE + " from " + TABLE_NAME; Cursor cursor = database.rawQuery(sql, null); List
provinceList = new ArrayList<>(); if (cursor != null) { while (cursor.moveToNext()) { String province = cursor.getString(cursor.getColumnIndex("province")); Log.i("tag", "province----->" + province); provinceList.add(province); } cursor.close(); } return provinceList; } /** * 查询指定省份或直辖市的所有市的集合 * * @param province 省份名称 * @return 查询到的所有城市 */ public List
getCities(String province) { String sql = "select distinct " + CITY + " from " + TABLE_NAME + " where " + PROVINCE + " = ?"; Cursor cursor = database.rawQuery(sql, new String[]{province}); List
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集合 * * @param city 市的名称 * @return 查询到的所有区 */ public List
getAreas(String city) { //获取指定市的所有区的列表的sql语句 String sql = "select distinct " + AREA + " from " + TABLE_NAME + " where " + CITY + " = ?"; Cursor cursor = database.rawQuery(sql, new String[]{city}); List
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; } }
首页
1
末页
去第
页
确定
Copyright ©2015 北京葡萄藤信息技术有限公司 All Rights Reserved | 京ICP备15035574号-1
32
3
1
10