Android 两个全国地址管理库的数据转换

情景:

    集成了一个lib, 里边作者有使用address.db作为数据来源!

    后台又给了一个数据库!

    两种解决方式:

    1:两个数据库进行转换

    2:直接使用新的数据库,然后修改第三方库代码

    采用了第一张:

数据库数据格式比较:

    


第一种解决思路:

1:    遍历第一个数据库   (黑色为后台给的数据库转成json的格式)

2:通过修改便利出来的字段,根据相关联系,进行手动修改字符串

3:写入另外一个数据库  (红色为 项目中原来使用的)


第一步实现:

    读取数据库每一条数据

    数据库位置:

        

    


    查询数据库中总条数:

    select count(*) from 表名

    

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

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * *************************************************
 * 项目名称:   bb_help0413
 *
 * @Author czsdandy
 * 创建时间:   2018/4/19  13:51
 * 用途
 * *************************************************
 */

public class DBManager {
    private String DB_NAME = "WAddressDB.db";
    private String DB_NAME_WRITE = "address.db";
    private Context mContext;

    public DBManager(Context mContext) {
        this.mContext = mContext;
    }

    //把assets目录下的db文件复制到dbpath下
    public SQLiteDatabase initDBReadManager(String packName) {
        String dbPath = "/data/data/" + packName
                + "/databases/" + DB_NAME;
        if (!new File(dbPath).exists()) {
            try {
                FileOutputStream out = new FileOutputStream(dbPath);
                InputStream in = mContext.getAssets().open("WAddressDB.db");
                byte[] buffer = new byte[1024];
                int readBytes = 0;
                while ((readBytes = in.read(buffer)) != -1)
                    out.write(buffer, 0, readBytes);
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return SQLiteDatabase.openOrCreateDatabase(dbPath, null);
    }

    //把assets目录下的db文件复制到dbpath下
    public SQLiteDatabase initDBWriteManager(String packName) {
        String dbPath = "/data/data/" + packName
                + "/databases/" + DB_NAME_WRITE;
        if (!new File(dbPath).exists()) {
            try {
                FileOutputStream out = new FileOutputStream(dbPath);
                InputStream in = mContext.getAssets().open("address.db");
                byte[] buffer = new byte[1024];
                int readBytes = 0;
                while ((readBytes = in.read(buffer)) != -1)
                    out.write(buffer, 0, readBytes);
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return SQLiteDatabase.openOrCreateDatabase(dbPath, null);
    }

    private SQLiteDatabase sqLiteDatabaseWriter ;

    //查询
    public ArrayList<City> query(SQLiteDatabase sqliteDB, String[] columns, String selection, String[] selectionArgs, SQLiteDatabase sqLiteDatabaseWriter) {
        this.sqLiteDatabaseWriter = sqLiteDatabaseWriter ;
        ArrayList<City> cities = new ArrayList<>();
        City city = null;
        try {
            String table = "addressTabble";
            Cursor cursor = sqliteDB.query(table, columns, selection, selectionArgs, null, null, null);
            System.out.print("开始了:" + System.currentTimeMillis());
            while (cursor.moveToNext()) {
                String code = cursor.getString(cursor.getColumnIndex("code"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                int level = cursor.getInt(cursor.getColumnIndex("level"));
                city = new City(code, name, level);
                cities.add(city);

                String id = "" ;
                String parentId = "";

                if (level == 1){
                    id =  code.substring(0 ,2);
//                    Log.e("a" , "id"+id);
                    parentId = "0";
//                    Log.e("a" , "parentId"+parentId);
                }else if (level == 2){
                    id = code.substring(0 , 4);
                    parentId =  code.substring(0 ,2);
                }else if (level == 3){
                    parentId = code.substring(0, 4);
//                    Log.e("a" , "3parentId"+parentId);
                    id = code ;
//                    Log.e("a" , "3id"+id);
                }

                String insertStr = "insert into address_dict(name,code,id,parentId)" +          //user_info为数据库表名
                        " values('" + city.getName() + "',"  + city.getCode() + "," + id + ","
                        + parentId  +")";
                sqLiteDatabaseWriter.execSQL(insertStr);
//        cursor.moveToNext();
            }
            System.out.print("结束l:" + System.currentTimeMillis());
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cities;
    }
}
 private DBManager dbManager ;
    private SQLiteDatabase sqLiteDatabaseWriter;
private SQLiteDatabase sqLiteDatabase;
  dbManager = new DBManager(this);
sqLiteDatabase = dbManager.initDBReadManager(getPackageName());
sqLiteDatabaseWriter = dbManager.initDBWriteManager(getPackageName());
 ArrayList<City> query = dbManager.query(this.sqLiteDatabase, null, null, null ,sqLiteDatabaseWriter);

以上就是完整的代码:

    第二步 :将代码写入到另外一个数据库中!

    参看 代码中有insert的语句

    一个语句如果在工具可以查询,那么在代码中就是可以查询的。

    中文需要添加'' (标红的部分,单引号!)进行转码


第三步:

    因为我们是读取assets中的文件, 然后将其读入到data/data 内存中

因此 需要把原来的文件复印出来

    方式:

        1:通过代码进行

        2:通过模拟器, 直接拿出我们转好数据,然后替换掉原来的数据库。

   一切正常!!!





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值