Android开发循序渐进实例4--数据库访问例子

xiaoxiao2021-03-01  72

在此实例中,重点展示使用Android平台提供的SQLite数据库访问方式,包括读写访问。1. 按照如下的配置以及在开发循序渐进实例1中描述的方法创建整个项目Base:project Name: ExampleFourPlatform: Android2.0;Application name: ExampleFourpackage name: com.exampleActivity: MainActivityResource 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);}@Overridepublic 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());}

@Overridepublic 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里面可以着力实现所有的数据访问代码,实现功能以及模块的统一化。

相关资源:Google Android开发入门与实战(09年度畅销榜TOP50)--详细书签版
转载请注明原文地址: https://www.6miu.com/read-3350087.html

最新回复(0)