/**
* 保存地址到数据库
*/
private void saveAddressData() {
String name = String.valueOf(tv_currency2.getText());
String address = String.valueOf(et_address.getText());
String remark = String.valueOf(et_remark.getText());
mDbHelper = new MyDBHelper2(this);
SQLiteDatabase sDb = mDbHelper.getWritableDatabase();
//先插入6条数据
String sql = "INSERT INTO " + MyDBHelper2.T_NAME + "(_name,address,remarks) VALUES('" + name + "','"
+ address + "','"
+ remark + "')";
sDb.execSQL(sql);
//每次操作后,查询数据库显示结果
queryAndShow();
}
/**
*查询数据库
*/
private void queryAndShow() {
String sql = "select * from " + MyDBHelper2.T_NAME;
StringBuffer sb = new StringBuffer();
Cursor cursor = mDbHelper.getWritableDatabase().rawQuery(sql, null);
while (cursor.moveToNext()) {
sb.append("_name=").append(cursor.getString(cursor.getColumnIndex(MyDBHelper2.TABLE_NAME))).append(" ")
.append("address=").append(cursor.getString(cursor.getColumnIndex(MyDBHelper2.TABLE_ADDRESS))).append(" ")
.append("remarks=").append(cursor.getInt(cursor.getColumnIndex(MyDBHelper2.TABLE_DESCRIBE))).append(" ")
.append("\n");
}
}
Android 保存数据到数据库
最新推荐文章于 2024-10-03 09:49:15 发布