}
}
创建的过程是在一个监听器里实现的,这里不再过多介绍。有了数据库就该创建表了,语句如下:
db.execSQL("create table cities(_id int, code varchar(10), city varchar(10)");
接下来是插入数据。在上面已经将要插入的数据存入到了一个二维数组中,在插入的时候考虑到乱码问题,特别设置了编码方式为 UTF-8 编码,然后逐条插入,代码如下:
class InsertListener implements OnClickListener {
public void onClick(View v) {
DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this,
"panda_weather");
SQLiteDatabase db = dbHelper.getWritableDatabase();
String d1 = "";
String d2 = "";
for (int i = 0; i < str.length; i++) {
d1 = str[i][0];
d2 = str[i][1];
String sql = "INSERT INTO cities(_id, code, city) values(" + (i+1) + ",?,?)";
db.beginTransaction();
try {
db.execSQL(sql, new String[] {new String(d1.getBytes(),"UTF-8"), new String(d2.getBytes(), "UTF-8")});
} catch (Exception e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
}
System.out.println("insert ========= OK ");
}
}
当控制台显示最后一句话的时候,就表示所有数据插入成功了,一共2564条数据。
接下来要进行的是创建 city 表,然后插入数据,这里将默认的城市设置为北京,城市码是 101010100 。详细过程不再描述,只给出执行语句:
db.execSQL("create table city(_id int, code varchar(10)");
String sql = "INSERT INTO city(_id, code) values(1, '101010100')";
到这里,整个数据库就创建完成了。数据库名为 panda_weather ,创建的两张表为 cities 和 city 。接下来检验数据库创建是否成功,把虚拟设备运行起来,进入到命令行提示符下,输入 adb shell 命令,进入到Android操作系统中,通过 cd 命令进入到存放数据库的目录,输入命令 sqlite3 panda_weather 后,进入到数据库模式下,然后输入 .schema 命令,查看数据库中是否包含 cityes 和 city 两张表,再用 select * from cities; 和 select * from city; 两条命令查看表中的记录是否正确。这里,看到的结果与插入的一致,表示数据库创建成功了。
5.4.2 数据库操作
数据库创建成功后,就投入到具体的使用了。经过分析可以知道,对数据库的操作主要包括数据库的初始化、由城市名得到对应的城市码、设置默认城市码和得到默认城市码这样四个操作。代码如下:
public class Database_Tools {
// 当前上下文
private Context _context;
// 构造函数
public Database_Tools(Context context) {
_context = context;
}
private String dbPath = "data/data/panda.weather/databases/"; // 数据库路径
private String dbName = "panda_weather"; // 数据库名称
/**
* 把数据库文件复制到指定目录
*/
public void init() {
try {
String DATABASE = dbPath + dbName;
File dir = new File(dbPath);
// 如果目录已经存在,则返回
if (dir.exists()) {
return;
}
// 创建目录
dir.mkdir();
// 如果数据库文件不存在,执行复制数据库文件
if (!(new File(DATABASE).exists())) {
// 获得封装 文件的InputStream对象
InputStream is = _context.getAssets().open("panda_weather");
FileOutputStream fos = new FileOutputStream(DATABASE);
// 下面这一段代码不是很理解,有待研究
byte[] buffer = new byte[8192];
int count = 0;
// 开始复制db文件
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 由城市名得到对应的城市码
* @param city
* @return
*/
public String getCityCode(String city) {
Database_Helper dbHelper = new Database_Helper(_context,
"panda_weather");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.query("cities", new String[] { "_id", "code", "city" },
"city=?", new String[] { new String(city.getBytes(),
"UTF-8") }, null, null, null);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String code = "";
while (cursor.moveToNext()) {
code = cursor.getString(cursor.getColumnIndex("code"));
}
db.close(); // 关闭数据库
return code;
}
/**
* 得到数据库中的默认城市 Code
* @return
*/
public String getDefaultCode() {
Database_Helper dbHelper = new Database_Helper(_context,
"panda_weather");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.query("city", new String[] { "_id", "code" }, "_id=?",
new String[] { "1" }, null, null, null);
} catch (Exception e) {
e.printStackTrace();
}
String city = "";
while (cursor.moveToNext()) {
city = cursor.getString(cursor.getColumnIndex("code"));
System.out.println("query ----> " + city);
}
cursor.close();
db.close(); // 释放数据库资源
Data_Key.defaultCode = city; // 给默认数据库赋值
return city
上一篇:
关于android手机的论文
下一篇:
试论APP广告在手机中的应用