你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发:启动应用的数据库初始化和版本更新

[日期:2011-08-18] 来源:Linux社区  作者:anskya520 [字体:大 中 小]

操作数据库的类:

  1. /** 
  2.  * 程序第一初始化的时候,通过类加载器把数据库拷贝到/data/data/<包名>/目录下 
  3.  * @author Administrator 
  4.  * 
  5.  */  
  6.   
  7.   
  8. public class AddressService {  
  9.     public static final String TAG="AddressService";  
  10.     Context context;  
  11.     public static final String DBNAME= "address.db";  
  12.       
  13.     public AddressService(Context context) {  
  14.         this.context = context;  
  15.     }  
  16.     /** 
  17.      * 判断数据库是否存在 
  18.      * @return 
  19.      */  
  20.     public boolean  isExist() {  
  21.         File file = new File(context.getFilesDir(),DBNAME);  
  22.         return file.exists();  
  23.     }  
  24.   
  25.     /** 
  26.      * 拷贝数据库到系统的/data/data/<包名>/下 
  27.      * @throws Exception 
  28.      */  
  29.     public void copyDataBase()  throws Exception{  
  30.         InputStream  is = this.getClass().getClassLoader().getResourceAsStream("address.db");  
  31.           
  32.         File file = new File(context.getFilesDir(),DBNAME);  
  33.         FileOutputStream fos = new FileOutputStream(file);  
  34.         byte[] buffer = new byte[1024];  
  35.         int len;  
  36.         while ((len = is.read(buffer))!=-1){  
  37.             fos.write(buffer, 0, len);  
  38.         };  
  39.         fos.close();  
  40.         is.close();  
  41.           
  42.     }  
  43.       
  44.     public String getAddress(String number){  
  45.         File file = new File(context.getFilesDir(),DBNAME);  
  46.         String address = number;  
  47.         SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null,SQLiteDatabase.OPEN_READONLY);  
  48.         if (db.isOpen()){ // 如果数据库是打开的,就可以对数据库进行操作,操作完后记得关闭数据库   
  49.             // ^1[358]\d{9}$    
  50.             if(Pattern.compile("^1[358]\\d{9}{1}quot;).matcher(number).matches()){  
  51.                 String phoneprefix7 = number.substring(0, 7);                 
  52.                 Log.i(TAG,phoneprefix7);  
  53.                 Cursor cursor= db.rawQuery("select city from info where mobileprefix=?", new String[]{phoneprefix7});  
  54.                 if(cursor.moveToFirst()){  
  55.                      address = cursor.getString(0);  
  56.                 }  
  57.                 cursor.close();  
  58.             }  
  59.             else{  
  60.                 Cursor cursor= null;  
  61.                 /* 固定电话 本地号码7位 ,8位 
  62.                 长途号码 带区号 区号有3位和4位的 
  63.                 10位 3位区号+7位电话号码  
  64.                 11位 4位区号+7位电话 3位区号+8位电话 
  65.                 12位 4位区号+8位电话*/  
  66.                                   
  67.                 switch (number.length()) {  
  68.                 case 7:  
  69.                     address = context.getApplicationContext().getResources().getString(R.string.local);  
  70.                     break;  
  71.                 case 8:  
  72.                     address = context.getApplicationContext().getResources().getString(R.string.local);  
  73.                     break;  
  74.                 case 10:  
  75.                     String numberprefix3 = number.substring(0, 3);  
  76.                     cursor= db.rawQuery("select city from info where area=?", new String[]{numberprefix3});  
  77.                     if(cursor.moveToFirst()){  
  78.                          address = cursor.getString(0);  
  79.                     }  
  80.                     cursor.close();  
  81.                     break;  
  82.                 case 11:  
  83.                     String numberprefix_3 = number.substring(0, 3);  
  84.                     String numberprefix_4 = number.substring(0, 4);                   
  85.                     cursor= db.rawQuery("select city from info where area=? or area = ?", new String[]{numberprefix_3,numberprefix_4});  
  86.                     if(cursor.moveToFirst()){  
  87.                          address = cursor.getString(0);  
  88.                     }  
  89.                     cursor.close();  
  90.                     break;  
  91.                 case 12:  
  92.                     String numberprefix4 = number.substring(0, 4);                    
  93.                     cursor= db.rawQuery("select city from info where area=?", new String[]{numberprefix4});  
  94.                     if(cursor.moveToFirst()){  
  95.                          address = cursor.getString(0);  
  96.                     }  
  97.                     cursor.close();  
  98.                     break;  
  99.                 case 4:  
  100.                     address = context.getApplicationContext().getResources().getString(R.string.emulator);  
  101.                     break;  
  102.                 }  
  103.                   
  104.             }  
  105.               
  106.             db.close();  
  107.         }  
  108.         return address;  
  109.     }  
  110.   
  111. }  
linux
相关资讯       Android开发 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 朱洋洋 发表于 2012/6/27 10:11:08
想问一下,你将数据库版本号定义在 string.xml文件里面了吗?String path = getApplicationContext().getResources().getString(
R.string.server_url); 得到的是服务器路径还是版本号?有点不懂。。。