在此实例中,重点展示使用Android平台提供的SQLite数据库访问方式,包括读写访问。
1. 按照如下的配置以及在开发循序渐进实例1中描述的方法创建整个项目Base:
project Name: ExampleFour
Platform: Android2.0;
Application name: ExampleFour
package name: com.example
Activity: MainActivity
Resource file: main.xml
一个TextView属性如下:
TextView
Id: @+id/ContentTextView
Text: Content:
一个EditView:
Id:@+id/EditContent
Text: blank
Layout width: fill_parent
二个Button:
Id: @+id/InsertButton
Text: Insert
Layout width: fill_parent
Id: @+id/ReadButton
Text: Read
Layout width: fill_parent
2. 在MainActivity中首先加入对SQLiteOpenHelper类的重载代码如下(内部类):
public class ExampleDBHelper extends SQLiteOpenHelper {
ExampleDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
StringBuffer sb = new StringBuffer();
sb.append("Create table ").append(TABLE_NAME);
sb.append("(").append(COLUMN_SN).append(" int not null, ");
sb.append(COLUMN_CONTENT).append(" text not null);");
arg0.execSQL(sb.toString());
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
3. 在MainActivity中加入如下的常量定义:
public static final int DB_VERSION = 1;
public static final String DB_NAME = "example_content";
public static final String TABLE_NAME = "et_content";
public static final String COLUMN_SN = "sn";
public static final String COLUMN_CONTENT = "content";
4. 在MainActivity中加入如下的变量定义:
private ExampleDBHelper dbHelper = null;
5. 撰写Button的事件响应代码:
private void connect_control_events() {
Button buttonInsert = (Button) findViewById(R.id.InsertButton);
buttonInsert.setOnClickListener(buttonInsert_listener);
Button buttonRead = (Button) findViewById(R.id.ReadButton);
buttonRead.setOnClickListener(buttonRead_listener);
}
private Button.OnClickListener buttonInsert_listener = new Button.OnClickListener() {
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
EditText contentControl = (EditText) findViewById(R.id.ContentEditText);
String strContent = contentControl.getEditableText().toString();
String[] columns = new String[2];
columns[0] = COLUMN_SN;
columns[1] = COLUMN_CONTENT;
Cursor cur = db.query(TABLE_NAME, columns, null, null, null, null, null);
if (cur.getCount() > 0) {
StringBuffer sb = new StringBuffer();
sb.append("update ").append(TABLE_NAME).append(" set ").append(COLUMN_CONTENT);
sb.append("=").append("'").append(strContent).append("'");
db.beginTransaction();
try {
db.execSQL(sb.toString());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} else {
StringBuffer sb = new StringBuffer();
sb.append("insert into ").append(TABLE_NAME).append("(").append(COLUMN_SN);
sb.append(",").append(COLUMN_CONTENT).append(")");
sb.append(" values(1, '").append(strContent).append("')");
db.beginTransaction();
try {
db.execSQL(sb.toString());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
cur.close();
}
}
};
private Button.OnClickListener buttonRead_listener = new Button.OnClickListener() {
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] columns = new String[2];
columns[0] = COLUMN_SN;
columns[1] = COLUMN_CONTENT;
Cursor cur = db.query(TABLE_NAME, columns, null, null, null, null, null);
if (cur.getCount() > 0) {
cur.moveToFirst();
String strContent = cur.getString(1);
EditText contentControl = (EditText) findViewById(R.id.ContentEditText);
contentControl.setText(strContent);
}
}
};
6. 在MainActivity的onCreate函数的末尾加入如下的关联代码:
dbHelper = new ExampleDBHelper(this.getBaseContext());
connect_control_events();
7. 测试,首先在EditText随机输入一些数据,点击Insert随即插入数据,然后停止本程序,重新启动程序,然后点击Read你发现EditText就是前一次输入的数据;
8. 改进,考虑到本程序是个例子,因此将对SQLiteOpenHelper重载放在MainActivity里面以内部类的形式来实现,如果在一个项目中,建议将此类独立出来形成XXXHelper,然后在此类当中实现SQLiteOpenHelper的重载(仍以内部类的形式来实现),这样在XXXHelper里面可以着力实现所有的数据访问代码,实现功能以及模块的统一化。