获取SQLLite省市区数据库中省市区数据的方法
2012-03-27 17:20
393 查看
在我上传的资源中有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;
}
}
废话少说,代码如下所示:
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;
}
}
相关文章推荐
- 获取SQLLite省市区数据库中省市区数据的方法
- 用http 中doGet 方法获取mysql 数据库数据的错误总结
- PHP实现在数据库百万条数据中随机获取20条记录的方法
- Symfony2实现从数据库获取数据的方法小结
- 四种数据库随机获取10条数据的方法
- 根据基础数据表id获取数据库中关联表字段内容方法
- ajax动态获取数据库中的数据方法
- jsp数据库基础之---使用post和get方法获取表单数据
- php从数据库中获取数据用ajax传送到前台的方法
- C#中联查的简单方法(如从数据库中获取到数据放到excel表中)
- ASP.NET连接数据库并获取数据方法总结
- 数据库存储过程中定义一个变量,通过sql语句在数据表中获取这个变量值的方法。
- JAVA 向数据库中插入数据并获取主键的方法(逐渐补充)
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
- jsp从数据库获取数据填充下拉框实现二级联动菜单的方法
- 四种数据库随机获取N条数据的方法
- php获取数据库中数据的实现方法
- ADO 操作数据库(三)--获取数据的各种方法
- PHP获取数据库表中的数据插入新的表再原删除数据方法
- symfony2实现从数据库获取数据的方法