@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
setAction();
}
/** 初始化View */
private void init() {
createDataBase = (Button) findViewById(R.id.create_database);
addData = (Button) findViewById(R.id.add_data);
updateData = (Button) findViewById(R.id.update_data);
deleteData = (Button) findViewById(R.id.delete_data);
queryData = (Button) findViewById(R.id.query_data);
replaceData = (Button) findViewById(R.id.replace_data);
/** 通过构造函数的参数将数据库信息指定成功;做数据库升级时要改版本号 1改为2 */
dbHelper = new MyDataBaseHelper(this, “BookStore.db”, null, 2);
}
/** 设置监听事件 */
private void setAction() {
createDataBase.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
addData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
/** 开始组装第一组数据 */
values.put(“name”, “the Da Vinci Code”);
values.put(“author”, “dan Brown”);
values.put(“pages”, 454);
values.put(“price”, 16.96);
/** 插入第一条数据 */
db.insert(“book”, null, values);
values.clear();
/** 开始组装第二组数据 */
values.put(“name”, “the Last Symbol”);
values.put(“author”, “dan Brown”);
values.put(“pages”, 510);
values.put(“price”, 19.96);
/** 插入第二条数据 */
db.insert(“book”, null, values);
Toast.makeText(MainActivity.this, “inset succeeded----”,
Toast.LENGTH_SHORT).show();
}
});
updateData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(“price”, 10.99);
/** 以下的意思是将名the Da Vinci Code这本书的价格改为10.99 */
db.update(“Book”, values, “name = ?”,
new String[] { “the Da Vinci Code” });
Toast.makeText(MainActivity.this, “update succeeded+++++++”,
Toast.LENGTH_SHORT).show();
}
});
deleteData.setOn
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
/** 删除页数超过500页的书籍 */
db.delete(“Book”, “pages > ?”, new String[] { “500” });
Toast.makeText(MainActivity.this, “delete succeeded$$$$$$$”,
Toast.LENGTH_SHORT).show();
}
});
queryData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
/** 查询表中所有的数据 */
Cursor cursor = db.query(“Book”, null, null, null, null, null,
null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor
.getColumnIndex(“name”));
String author = cursor.getString(cursor
.getColumnIndex(“author”));
int pages = cursor.getInt(cursor
.getColumnIndex(“pages”));
double price = cursor.getDouble(cursor
.getColumnIndex(“price”));
} while (cursor.moveToNext());
}
cursor.close();
Toast.makeText(MainActivity.this, “query succeeded@@@@@@”,
Toast.LENGTH_SHORT).show();
}
});
/** 使用事务进行数据的更换,这样会比较安全 */
replaceData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
/** 开启事务 作用:删除表里面的数据的时,要么成功,要么失败,两个事件只能同时完成 */
db.beginTransaction();
try {
db.delete(“Book”, null, null);
if (true) {
/** 这里抛出一个异常,让事务失败 */
throw new NullPointerException();
}
@SuppressWarnings(“unused”)
ContentValues values = new ContentValues();
values.put(“name”, “Game of Thrones”);
values.put(“author”, “George Martin”);
values.put(“pages”, 720);
values.put(“price”, 20.55);
db.insert(“Book”, null, values);
/** 事务已经执行完成 */
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();