国家统计局公布的最新县及县以上行政区划代码的网址(最新数据截止2013.08.31):
http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html
利用其规律将这些数据转为想要的sql语句.
需要注意的地方是直辖市的市辖区和县的操作,以及省直辖县级行政区划和自治区直辖县级行政区划的操作
直接上代码, 我为了学习学习新知识, 代码用 java7 写的
private static final int TYPE_PRIVINCE = 1;
private static final int TYPE_CITY = 2;
private static final int TYPE_REGION = 3;
public static void main(String[] args) {
StringBuilder sbProvince = new StringBuilder();
StringBuilder sbCity = new StringBuilder();
StringBuilder sbRegion = new StringBuilder();
final String CR = System.getProperty("line.separator");// 换行符
String currentProvinceCode = null;// 当前省code
String currentProvinceName = null;// 当前省名字
String currentCityCode = null;// 当前市
String currentCityName = null;// 当前城市名字
int type = TYPE_PRIVINCE;
boolean isProvinceRegion = false;// 是否为省直辖县级行政区划
// 读取文件
Path path = Paths.get("E:\\省市区信息.txt");
try (BufferedReader reader = Files.newBufferedReader(path,
Charset.forName("GBK"))) {// try-with-resource
String line = null;
while ((line = reader.readLine()) != null) {
if (!isEmpty(line)) {
String[] segments = line.trim().split(" ");// 去掉首尾空字符
String code = "";// 代号
String name = "";// 名字
code = segments[0];
name = segments[segments.length - 1];
type = getType(code);
if (type == TYPE_PRIVINCE) {
currentProvinceCode = code;
currentProvinceName = name;
// 格式化插入省表的语句
String insertProvinceSql = getProvinceSql(code, name);
sbProvince.append(insertProvinceSql).append(CR);
// 添加省份注释信息
sbCity.append("//").append(currentProvinceName)
.append(CR);
} else if (type == TYPE_CITY) {
isProvinceRegion = false;
// 源数据对于直辖市下面分为市辖区和县
// 将市名处理成和直辖市名字一样
if ("县".equals(name)) {
continue;
}
if ("市辖区".equals(name)) {
name = currentProvinceName;// 直辖市,同省
}
if(name.contains("直辖县级行政区划")){//省直辖县级行政区划,自治区直辖县级行政区划
isProvinceRegion = true;
continue;
}else {
isProvinceRegion = false;
}
currentCityCode = code;
currentCityName = name;
// 格式化插入市表的语句
String insertCitySql = getCitySql(code, name,
currentProvinceCode);
sbCity.append(insertCitySql).append(CR);
// 添加省市注释信息
sbRegion.append("//").append(currentProvinceName)
.append(currentCityName).append(CR);
} else if (type == TYPE_REGION) {
if ("市辖区".equals(name)) {// 去掉市辖区,没用的数据
continue;
}
if (isProvinceRegion) {
// 省直辖县级行政区划操作同市
currentCityCode = code;
currentCityName = name;
// 格式化插入市表的语句
String insertCitySql = getCitySql(code, name,
currentProvinceCode);
sbCity.append(insertCitySql).append(CR);
// 添加省市注释信息
sbRegion.append("//").append(currentProvinceName)
.append(name).append(CR);
} else {
// 格式化插入区表的语句
String insertRegionSql = getRegionSql(code, name,
currentCityCode);
sbRegion.append(insertRegionSql).append(CR);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("//插入省份数据");
System.out.println(sbProvince.toString());
System.out.println("//插入市数据");
System.out.println(sbCity.toString());
System.out.println("//插入区县数据");
System.out.println(sbRegion.toString());
}
private static final String insertRegionSql = "db.execSQL(\"insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('%s','%s','%s')\");";
private static String getRegionSql(String code, String name, String cityCode) {
return String.format(insertRegionSql, code, name, cityCode);
}
private static final String insertCitySql = "db.execSQL(\"insert into CITY('CITY_ID','CITY_NAME','PROVINCE_ID') values('%s','%s','%s')\");";
private static String getCitySql(String code, String name,
String provinceCode) {
return String.format(insertCitySql, code, name, provinceCode);
}
private static final String insertProvinceSql = "db.execSQL(\"insert into PROVINCE('PROVINCE_NAME','PROVINCE_ID') values('%s','%s')\");";
private static String getProvinceSql(String code, String name) {
return String.format(insertProvinceSql, name, code);
}
private static int getType(final String code) {
if (isEmpty(code)) {
return -1;
}
if (code.endsWith("0000")) {// 末尾4个0为省
return TYPE_PRIVINCE;
}
if (code.endsWith("00")) {// 末尾2个0为市
return TYPE_CITY;
}
return TYPE_REGION;
}
private static boolean isEmpty(String str) {
if (str == null || str.length() == 0) {
return true;
} else {
return false;
}
}
操作结果,拿北京市的信息出来看一下:
//插入省份数据
db.execSQL("insert into PROVINCE('PROVINCE_NAME','PROVINCE_ID') values('北京市','110000')");
//插入市数据
//北京市
db.execSQL("insert into CITY('CITY_ID','CITY_NAME','PROVINCE_ID') values('110100','北京市','110000')");
//插入区县数据
//北京市北京市
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110101','东城区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110102','西城区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110105','朝阳区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110106','丰台区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110107','石景山区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110108','海淀区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110109','门头沟区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110111','房山区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110112','通州区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110113','顺义区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110114','昌平区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110115','大兴区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110116','怀柔区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110117','平谷区','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110228','密云县','110100')");
db.execSQL("insert into REGION('REGION_ID','REGION_NAME','CITY_ID') values('110229','延庆县','110100')");
先讲统计局的数据拷贝保存到文件,然后将代码的insertRegionSql,insertCitySql,insertProvinceSql三句改为自己想要的格式即可.
原始数据, sql, java代码都可以点这个链接下载:
http://pan.baidu.com/s/1sj4rqKP