Android学习笔记(三数据库SQLITE的基本操作)(2)
来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:44 点击:次
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(listener1);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(listener2);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(listener3);
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(listener4);
button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(listener5);
}
private void prepareListener() {
listener1 = new OnClickListener() {
public void onClick(View v) {
CreateTable();
}
};
listener2 = new OnClickListener() {
public void onClick(View v) {
dropTable();
}
};
listener3 = new OnClickListener() {
public void onClick(View v) {
insertItem();
}
};
listener4 = new OnClickListener() {
public void onClick(View v) {
deleteItem();
}
};
listener5 = new OnClickListener() {
public void onClick(View v) {
showItems();
}
};
}
private void CreateTable() {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
Log.i("haiyang:createDB=", sql);
try {
db.execSQL("DROP TABLE IF EXISTS diary");
db.execSQL(sql);
setTitle("数据表成功重建");
} catch (SQLException e) {
setTitle("数据表重建错误");
}
}
private void dropTable() {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "drop table " + TABLE_NAME;
try {
db.execSQL(sql);
setTitle("数据表成功删除:" + sql);
} catch (SQLException e) {
setTitle("数据表删除错误");
}
}
private void insertItem() {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql1 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY
+ ") values('haiyang', 'android的发展真是迅速啊');";
String sql2 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>