-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBHelper.java
More file actions
111 lines (100 loc) · 3.39 KB
/
DBHelper.java
File metadata and controls
111 lines (100 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.example.tester;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by toeknee on 13-8-1.
*/
public class DBHelper extends SQLiteOpenHelper {
/**
* 构建一个数据库操作对象
* @param context 当前程序的上下文对象
* @param dataname 数据库名
*/
public DBHelper(Context context, String dataname){
super(context, dataname, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* 得到所有支持的省份或直辖市名称的String类型数组
* @return 支持的省份或直辖市数组
*/
public String[] getAllProvinces() {
String[] columns={"name"};
SQLiteDatabase db = getReadableDatabase();
//查询获得游标
Cursor cursor = db.query("provinces", columns, null, null, null, null, null);
columns = null;
int count= cursor.getCount();
String[] provinces = new String[count];
count = 0;
while(!cursor.isLast()) {
cursor.moveToNext();
provinces[count] = cursor.getString(0);
Log.d("GetProvinces", provinces[count]);
count ++;
}
cursor.close();
db.close();
return provinces;
}
/**
* 根据省份数组来得到对应装有对应的城市名和城市编码的列表对象
* @param provinces 省份数组
* @return 索引0为对应的城市名的二维数组和索引1为对应城市名的二维数组
*/
public List<String[][]> getAllCityAndCode(String[] provinces) {
int length= provinces.length;
String[][] city = new String[length][];
String[][] code = new String[length][];
int count = 0;
SQLiteDatabase db = getReadableDatabase();
for(int i=0; i<length; i++) {
Cursor cursor = db.query("citys", new String[]{"name", "city_num"},
"province_id = ? ", new String[]{String.valueOf(i)}, null, null, null);
count = cursor.getCount();
city[i] = new String[count];
code[i] = new String[count];
count = 0;
while(!cursor.isLast()) {
cursor.moveToNext();
city[i][count] = cursor.getString(0);
code[i][count] = cursor.getString(1);
count ++;
}
cursor.close();
}
db.close();
List<String[][]> result = new ArrayList<String[][]>();
result.add(city);
result.add(code);
return result;
}
/**
* 由城市名查询数据库来得到城市码
* @param cityName 城市名
* @return 城市码
*/
public String getCityCodeByName(String cityName) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query("citys", new String[]{"city_num"},
"name = ? ", new String[]{cityName}, null, null, null);
String cityCode = null;
if(!cursor.isLast()){
cursor.moveToNext();
cityCode = cursor.getString(0);
}
cursor.close();
db.close();
return cityCode;
}
}