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.setOnClickListener(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();
} finally {
/** 结束事务,在finally里面结束,无论如何都会执行到这边的 */
db.endTransaction();
}
}
});
}
}
直接上代码
import android.content.Context;
import android.database.sqlite.SQLiteDatab
ase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
*
-
@项目名:DataBaseTest
-
@类名称:MyDataBaseHelper
-
@类描述: 对数据库的创建和操作
-
@创建人:HXF
-
@修改人:
-
@创建时间:2015-8-4 上午9:07:02
teDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
*
-
@项目名:DataBaseTest
-
@类名称:MyDataBaseHelper
-
@类描述: 对数据库的创建和操作
-
@创建人:HXF
-
@修改人:
-
@创建时间:2015-8-4 上午9:07:02