zoukankan      html  css  js  c++  java
  • 获取SQLLite省市区数据库中省市区数据的方法

              在我上传的资源中有SQLLite版全国省市区的数据库,现在我来介绍下我开发地址列表时获取省市区数据时的方法。

    废话少说,代码如下所示:

    package xxxx.com.common;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

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

    public class AddressUtil {

        //获取省的地址列表,//file-->数据库文件
        public static Map<Integer,List> getProvince(File file){
            
            String sql = "select ProSort ,ProName from T_Province ";
            SQLiteDatabase db = null;
            Cursor c = null;
            Map<Integer,List> provinceData = new HashMap<Integer,List>();
            //List provinceList = null;
            try{
                    db = SQLiteDatabase.openOrCreateDatabase(file, null);
                    c = db.rawQuery(sql, null);
                    List provinceList1 = new ArrayList();
                    List provinceList2 = new ArrayList();
                    while(c.moveToNext()){
                            Map provinceMap = new HashMap();
                            provinceMap.put(c.getString(1), c.getInt(0));
                            provinceList1.add(provinceMap);
                            provinceList2.add(c.getString(1));
                    }
                    provinceData.put(0, provinceList1);
                    provinceData.put(1, provinceList2);
            }catch(Exception e){
                    Log.d("WineStock", "getProvince:"+e.getMessage());
            }finally{
                    if(c!=null){
                            c.close();
                    }
                    if(db!=null){
                            db.close();
                    }
            }
            return provinceData;
        }
        //获取对应省下面城市的列表,//file-->数据库文件,id-->指对应省的ID
        public static Map<Integer,List> getCityByPid(int id,File file){
            String sql = "select ProID,CityName  from T_City where ProID= "+id;
            SQLiteDatabase db = null;
            Cursor c = null;
            Map<Integer,List> cityData = new HashMap<Integer,List>();
            //List cityList = null;
            try{
                    db = SQLiteDatabase.openOrCreateDatabase(file, null);
                    c = db.rawQuery(sql, null);
                    List cityList1 = new ArrayList();
                    List cityList2 = new ArrayList();
                    while(c.moveToNext()){
                            Map cityMap = new HashMap();
                            cityMap.put(c.getString(1), c.getInt(0));
                            cityList1.add(cityMap);
                            cityList2.add(c.getString(1));
                    }
                    cityData.put(0, cityList1);
                    cityData.put(1, cityList2);
                    
            }catch(Exception e){
                Log.d("WineStock", "getCityByPid:"+e.getMessage());
            }finally{
                    if(c!=null){
                            c.close();
                    }
                    if(db!=null){
                            db.close();
                    }
            }
            return cityData;
        }
        //获取对应市下面区的列表,//file-->数据库文件,id-->指对应市的ID
        public static List<String> getAreaByPid(int id,File file){
            String sql = "select ZoneName  from T_Zone where CityID= "+id;
            SQLiteDatabase db = null;
            Cursor c = null;
            List<String> areaList = null;
            try{
                    db = SQLiteDatabase.openOrCreateDatabase(file, null);
                    c = db.rawQuery(sql, null);
                    areaList = new ArrayList<String>();
                    while(c.moveToNext()){
                        areaList.add(c.getString(0));
                    }
            }catch(Exception e){
                Log.d("WineStock", "getAreaByPid:"+e.getMessage());
            }finally{
                    if(c!=null){
                            c.close();
                    }
                    if(db!=null){
                            db.close();
                    }
            }
            return areaList;
        }
        
    }



  • 相关阅读:
    Leetcode 16.25 LRU缓存 哈希表与双向链表的组合
    Leetcode437 路径总和 III 双递归与前缀和
    leetcode 0404 二叉树检查平衡性 DFS
    Leetcode 1219 黄金矿工 暴力回溯
    Leetcode1218 最长定差子序列 哈希表优化DP
    Leetcode 91 解码方法
    Leetcode 129 求根到叶子节点数字之和 DFS优化
    Leetcode 125 验证回文串 双指针
    Docker安装Mysql记录
    vmware虚拟机---Liunx配置静态IP
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2426885.html
Copyright © 2011-2022 走看看