读取第三方数据库时打开数据库失败

只怪時間沖淡了緣分 发布于 2016/08/23 19:08
阅读 202
收藏 0

为什么打开数据库失败,找不到在哪里出问题了

代码:

package com.itcpf.mobilesafe.engine;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AddressDao {
    //1,指定访问数据库的路径
    public static String  path = "data/data/com.itcpf.mobilesafe/address.db" ;
                                 
    private static String maddress = "未知号码";
    public static String getAddress(String phone){
        //正则表达式,匹配手机号码
        //手机号码的正则表达式
        String regularExpression = "^1[3-8]\\d{9}";
        //2,开启数据库连接(只读的形式打开)
        //SQLiteDatabase.openDatabase(regularExpression, factory, flags)
        //regularExpression 数据库路径   factory 游标工厂 (在此处没意义)  flags 操作权限
         SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
        if(phone.matches(regularExpression)){
            //查询数据库只用到前七位 
            phone = phone.substring(0, 7);
            //3,数据库查询
            Cursor cursor = db.query("data1", new String[]{"outkey"}, "id = ?", new String[]{phone}, null, null, null);
            //查到即可
            if(cursor.moveToNext()){
                String outkey = cursor.getString(0);
                //5,通过data1查询到的结果,作为外键查询data2
                Cursor indexcursor = db.query("data2", new String[]{"location"}, "id = ?", new String[]{outkey}, null, null, null);
                if(indexcursor.moveToNext()){
                    maddress  = indexcursor.getString(0);
                    
                }
            }else{
                maddress = "未知号码";
            }
        }else{
            int length = phone.length();
            switch (length) {
            case 3://119 110 120 114
                maddress = "报警电话";
                break;
            case 4://5554 5556
                maddress = "模拟器";
                break;
            case 5://10086 99555
                maddress = "服务电话";
                break;
            case 7:
                maddress = "本地电话";
                break;
            case 8:
                maddress = "本地电话";
                break;
            case 11:
                //(3+8) 区号+座机号码(外地),查询data2
                String area = phone.substring(1, 3);
                Cursor cursor = db.query("data2", new String[]{"location"}, "area = ?", new String[]{area}, null, null, null);
                if(cursor.moveToNext()){
                    maddress = cursor.getString(0);
                }else{
                    maddress = "未知号码";
                }
                break;
            case 12:
                //(4+8) 区号(0791(江西南昌))+座机号码(外地),查询data2
                String area1 = phone.substring(1, 4);
                Cursor cursor1 = db.query("data2", new String[]{"location"}, "area = ?", new String[]{area1}, null, null, null);
                if(cursor1.moveToNext()){
                    maddress = cursor1.getString(0);
                }else{
                    maddress = "未知号码";
                }
                break;
            }
        }
        return maddress;
    
    }
}
加载中
返回顶部
顶部