package com.msquirrel.activity;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.demo_sqlite.R;
import com.msquirrel.model.Person;
/**
* 执行 创建数据库、增加数据、删除数据、修改数据、查询数据。
*
* @author msquirrel
*
*/
public class SQLiteActivity extends Activity {
private Button btnCteateDataBase = null;
private Button btnAdd = null;
private Button btnUpdate = null;
private Button btnQuery = null;
private Button btnDelete = null;
private SQLiteDatabase db = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite);
// 打开或创建test.db数据库
db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
initView();
setListener();
}
private void initView() {
// TODO Auto-generated method stub
btnCteateDataBase = (Button) findViewById(R.id.btn_create_database);
btnAdd = (Button) findViewById(R.id.btn_add);
btnUpdate = (Button) findViewById(R.id.btn_update);
btnQuery = (Button) findViewById(R.id.btn_query);
btnDelete = (Button) findViewById(R.id.btn_delete);
}
private void setListener() {
// TODO Auto-generated method stub
btnCteateDataBase.setOnClickListener(new myOnClickListener());
btnAdd.setOnClickListener(new myOnClickListener());
btnUpdate.setOnClickListener(new myOnClickListener());
btnQuery.setOnClickListener(new myOnClickListener());
btnDelete.setOnClickListener(new myOnClickListener());
}
private class myOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_create_database:
db.execSQL("DROP TABLE IF EXISTS person");
// 创建person表
db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");
break;
case R.id.btn_add:
Person person = new Person();
person.setName("john");
person.setAge(19);
// 插入数据
db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)",
new Object[] { person.getName(), person.getAge() });
break;
case R.id.btn_update:
Person person2 = new Person();
person2.setName("john");
person2.setAge(20);
ContentValues cv = new ContentValues();
cv.put("age", person2.getAge()); //修改内容
db.update("person", cv, "name = ?",
new String[] { person2.getName() });
break;
case R.id.btn_query:
Cursor c = db.rawQuery("select * from person where name=?",new String[]{"john"});
while(c.moveToNext()){
int id = Integer.parseInt(c.getString(c.getColumnIndex("_id")));
String name =c.getString(c.getColumnIndex("name"));
String age = c.getString(c.getColumnIndex("age"));
Log.v("Test ", "id:" + id + " name: " + name + " age: " + age);
}
break;
case R.id.btn_delete:
String whereClause = "_id=?";//删除的条件
String[] whereArgs = {"1"};//删除的条件参数
db.delete("person",whereClause,whereArgs);//执行删除
break;
default:
break;
}
}
}
}
|