- 将格式为db的文件拷贝到assets中;
- 将文件拷贝到/data/data/项目包名/databases/xx.db;
// 将assets中的文件拷贝到系统databases目录下
publicclassDbUtils{
@SuppressLint("SdCardPath")
publicstaticvoid packDataBase(Context context,String dbName){
File file = context.getDatabasePath(dbName);
try{
// 判断下文件是否存在,存在就不用重新拷贝了
if(!file.exists()){
OutputStream os =newFileOutputStream(file);
InputStream is = context.getAssets().open(dbName);
// 文件写入
byte[] buffer =newbyte[1024];
int length;
while((length = is.read(buffer))>0){
os.write(buffer,0, length);
}
// 关闭文件流
os.flush();
os.close();
is.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
- 自定义SQLLiteHlpter类,创建一个名字和步骤1中db名称一样的数据库;
- 增删改查
- 电话号码查询归属地示例
publicclassAddressDao{
publicstaticString mAddress ="未知号码";
publicstaticString getAddress(String phone){
mAddress ="未知号码";
DbManager.DaoConfig daoConfig =newDbManager.DaoConfig().setDbName(Constant.DB_ADDRESS)
.setDbVersion(1);
DbManager db = x.getDb(daoConfig);
//正则表达式,匹配手机号码
String regularExpression ="^1[3-8]\\d{9}";
if(phone.matches(regularExpression)){
// 处理手机号码
phone = phone.substring(0,7);
String sql ="select s.location from data1 t left join data2 s on t.outkey=s.id where t.id =? order by s.id;";
SqlInfo sqlInfo =newSqlInfo(sql);
sqlInfo.addBindArg(newKeyValue("id",phone));//key值可以随便写
try{
Cursor cursor = db.execQuery(sqlInfo);
if(cursor.moveToNext()){
mAddress = cursor.getString(0);
}
}catch(DbException e){
e.printStackTrace();
}
}else{
int length = phone.length();
switch(length){
case3://119 110 120 114
if("110".equals(phone)){
mAddress ="报警电话";
}elseif("114".equals(phone)){
mAddress ="查号电话";
}elseif("119".equals(phone)){
mAddress ="火警电话";
}elseif("120".equals(phone)){
mAddress ="抢救电话";
}
break;
case4://5556,5554
mAddress ="模拟器";
break;
case5://10086 99555
mAddress ="服务电话";
break;
case7:
mAddress ="本地电话";
break;
case8:
mAddress ="本地电话";
break;
case11:
//(3+8) 区号+座机号码(外地),查询data2
String area = phone.substring(1,3);
String sql ="select location from data2 where area=?";
SqlInfo sqlInfo =newSqlInfo(sql);
sqlInfo.addBindArg(newKeyValue("area",area));
try{
Cursor cursor = db.execQuery(sqlInfo);
if(cursor.moveToNext()){
String temp = cursor.getString(0);
mAddress= getLocation(temp);
}
}catch(DbException e){
e.printStackTrace();
}
break;
case12:
//(4+8) 区号(0791(江西南昌))+座机号码(外地),查询data2
String area1 = phone.substring(1,4);
String sql1 ="select location from data2 where area=?";
SqlInfo sqlInfo1 =newSqlInfo(sql1);
sqlInfo1.addBindArg(newKeyValue("area",area1));
try{
Cursor cursor = db.execQuery(sqlInfo1);
if(cursor.moveToNext()){
String temp = cursor.getString(0);
mAddress= getLocation(temp);
}
}catch(DbException e){
e.printStackTrace();
}
break;
}
}
return mAddress;
}
privatestaticString getLocation(String location){
String result ="";
if(location.contains("移动")){
result = location.substring(0,location.indexOf("移动"));
}elseif(location.contains("联通")){
result = location.substring(0,location.indexOf("联通"));
}elseif(location.contains("电信")){
result = location.substring(0, location.indexOf("电信"));
}
return result+"固定电话";
}
}
时间: 2024-10-10 01:28:53