码迷,mamicode.com
首页 > 数据库 > 详细

db文件的读取

时间:2016-11-13 11:33:41      阅读:604      评论:0      收藏:0      [点我收藏+]

标签:index   const   文件流   报警   数据库   系统   dao   lin   case   

  1. 将格式为db的文件拷贝到assets中;
  2. 将文件拷贝到/data/data/项目包名/databases/xx.db;
    1. // 将assets中的文件拷贝到系统databases目录下
    2. publicclassDbUtils{
    3. @SuppressLint("SdCardPath")
    4. publicstaticvoid packDataBase(Context context,String dbName){
    5. File file = context.getDatabasePath(dbName);
    6. try{
    7. // 判断下文件是否存在,存在就不用重新拷贝了
    8. if(!file.exists()){
    9. OutputStream os =newFileOutputStream(file);
    10. InputStream is = context.getAssets().open(dbName);
    11. // 文件写入
    12. byte[] buffer =newbyte[1024];
    13. int length;
    14. while((length = is.read(buffer))>0){
    15. os.write(buffer,0, length);
    16. }
    17. // 关闭文件流
    18. os.flush();
    19. os.close();
    20. is.close();
    21. }
    22. }catch(IOException e){
    23. e.printStackTrace();
    24. }
    25. }
    26. }
     
  3. 自定义SQLLiteHlpter类,创建一个名字和步骤1中db名称一样的数据库;
  4. 增删改查
  5. 电话号码查询归属地示例
    1. publicclassAddressDao{
    2. publicstaticString mAddress ="未知号码";
    3. publicstaticString getAddress(String phone){
    4. mAddress ="未知号码";
    5. DbManager.DaoConfig daoConfig =newDbManager.DaoConfig().setDbName(Constant.DB_ADDRESS)
    6. .setDbVersion(1);
    7. DbManager db = x.getDb(daoConfig);
    8. //正则表达式,匹配手机号码
    9. String regularExpression ="^1[3-8]\\d{9}";
    10. if(phone.matches(regularExpression)){
    11. // 处理手机号码
    12. phone = phone.substring(0,7);
    13. String sql ="select s.location from data1 t left join data2 s on t.outkey=s.id where t.id =? order by s.id;";
    14. SqlInfo sqlInfo =newSqlInfo(sql);
    15. sqlInfo.addBindArg(newKeyValue("id",phone));//key值可以随便写
    16. try{
    17. Cursor cursor = db.execQuery(sqlInfo);
    18. if(cursor.moveToNext()){
    19. mAddress = cursor.getString(0);
    20. }
    21. }catch(DbException e){
    22. e.printStackTrace();
    23. }
    24. }else{
    25. int length = phone.length();
    26. switch(length){
    27. case3://119 110 120 114
    28. if("110".equals(phone)){
    29. mAddress ="报警电话";
    30. }elseif("114".equals(phone)){
    31. mAddress ="查号电话";
    32. }elseif("119".equals(phone)){
    33. mAddress ="火警电话";
    34. }elseif("120".equals(phone)){
    35. mAddress ="抢救电话";
    36. }
    37. break;
    38. case4://5556,5554
    39. mAddress ="模拟器";
    40. break;
    41. case5://10086 99555
    42. mAddress ="服务电话";
    43. break;
    44. case7:
    45. mAddress ="本地电话";
    46. break;
    47. case8:
    48. mAddress ="本地电话";
    49. break;
    50. case11:
    51. //(3+8) 区号+座机号码(外地),查询data2
    52. String area = phone.substring(1,3);
    53. String sql ="select location from data2 where area=?";
    54. SqlInfo sqlInfo =newSqlInfo(sql);
    55. sqlInfo.addBindArg(newKeyValue("area",area));
    56. try{
    57. Cursor cursor = db.execQuery(sqlInfo);
    58. if(cursor.moveToNext()){
    59. String temp = cursor.getString(0);
    60. mAddress= getLocation(temp);
    61. }
    62. }catch(DbException e){
    63. e.printStackTrace();
    64. }
    65. break;
    66. case12:
    67. //(4+8) 区号(0791(江西南昌))+座机号码(外地),查询data2
    68. String area1 = phone.substring(1,4);
    69. String sql1 ="select location from data2 where area=?";
    70. SqlInfo sqlInfo1 =newSqlInfo(sql1);
    71. sqlInfo1.addBindArg(newKeyValue("area",area1));
    72. try{
    73. Cursor cursor = db.execQuery(sqlInfo1);
    74. if(cursor.moveToNext()){
    75. String temp = cursor.getString(0);
    76. mAddress= getLocation(temp);
    77. }
    78. }catch(DbException e){
    79. e.printStackTrace();
    80. }
    81. break;
    82. }
    83. }
    84. return mAddress;
    85. }
    86. privatestaticString getLocation(String location){
    87. String result ="";
    88. if(location.contains("移动")){
    89. result = location.substring(0,location.indexOf("移动"));
    90. }elseif(location.contains("联通")){
    91. result = location.substring(0,location.indexOf("联通"));
    92. }elseif(location.contains("电信")){
    93. result = location.substring(0, location.indexOf("电信"));
    94. }
    95. return result+"固定电话";
    96. }
    97. }
     





db文件的读取

标签:index   const   文件流   报警   数据库   系统   dao   lin   case   

原文地址:http://www.cnblogs.com/his365/p/6058174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!