public static String getAddressByPhoneNumber(String number) { String address = number; SQLiteDatabase database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); // 正则表达式 匹配号码 // // 手机号码 13 14 15 16 18 // ^1[34568]/d{9} if (number.matches("^1[34568]\\d{9}$")) { Cursor cursor = database .rawQuery( "select location from data2 where id = (select outkey from data1 where id = ?)", new String[] { number.substring(0, 7) }); while (cursor.moveToNext()) { String location = cursor.getString(0); address = location; } cursor.close(); } else { switch (number.length()) { case 3: address = "匪警电话"; break; case 4: // 5554 address = "模拟器"; break; case 5: // 10086 address = "客服电话"; break; case 7: // address = "本地号码"; break; case 8: address = "本地号码"; break; default: // /处理长途电话 10 if (number.length() > 10 && number.startsWith("0")) { // 010-59790386 Cursor cursor = database.rawQuery( "select location from data2 where area = ?", new String[] { number.substring(1, 3) }); while (cursor.moveToNext()) { String location = cursor.getString(0); address = location.substring(0, location.length() - 2); } cursor.close(); // 0855-59790386 cursor = database.rawQuery( "select location from data2 where area = ?", new String[] { number.substring(1, 4) }); while (cursor.moveToNext()) { String location = cursor.getString(0); address = location.substring(0, location.length() - 2); } } break; } } return address; }
//1.打开数据库 path 为 数据库的完整目录 第三个参数是 操作的 模式 只读 只写 之类的
SQLiteDatabase database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
//2. 得到一个Cursor 第一个参数是 SQL语句 第二个参数是 ?表示的内容
Cursor cursor = database.rawQuery("select location from data2 where id = (select outkey from data1 where id = ?)",new String[] { number.substring(0, 7) };
//3. 对查询到的 cursor 进行 取出
while (cursor.moveToNext()) { <span style="white-space:pre"> </span>String location = cursor.getString(0);
<span style="white-space:pre"> </span>address = location.substring(0, location.length() - 2);
<span style="font-family: Arial, Helvetica, sans-serif;">}</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span>
<span style="font-family:Arial, Helvetica, sans-serif;">cursor.close();</span>