sqlite:轻量级的关系型数据库,android提供sqliteOpenHelper帮助类,方便对数据库创建和升级。抽象方法onCreate()和 onUpgrade(),创建和升级
数据库逻辑。
实例方法getReadableDatabase() 和
getWritableDatabase(),创建或打开现有数据库。
public class MyDatabaseHelper extends sqliteOpenHelper {
public static final String CREATE_BOOK = "create table Book ("
+ "id integer primary key autoincrement,"
+ "author text,"
+ "price real,"
+ "pages integer,"
+ "name text)";
public static final String CREATE_CATEGORY = "create table Category ("
+ "id integer primary key autoincrement,"
+ "category_name text,"
+ "category_code integer)";
private Context mContext;
//重写构造方法:Context,数据库名,查询数据时返回自定义Cursor,数据库版本号
public MyDatabaseHelper(Context context,String name,CursorFactory factory,int version) {
super(context,name,factory,version);
mContext = context;
}
@Override
public void onCreate(sqliteDatabase db) {
db.execsql(CREATE_BOOK);
db.execsql(CREATE_CATEGORY);
Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
db.execsql("drop table if exists Book");
db.execsql("drop table if exists Category");
onCreate(db);
}
}
1、创建数据库:
dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
Button createDatabase = (Button) findViewById(R.id.create_database);
createDatabase.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
生成BookStore.db数据库
使用sqliteSpy软件打开,可以看到Book表,里面有5种数据类型
2、添加数据
Button addData = (Button) findViewById(R.id.add_data);
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 Lost Symbol");
values.put("author",510);
values.put("price",19.95);
db.insert("Book",values);
}
});
可以看到数据库里面的数据
3、更新数据
Button updateData = (Button) findViewById(R.id.update_data);
updateData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sqliteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price",10.99);
db.update("Book",values,"name = ?",new String[] { "The Da Vinci Code" });
}
});
更新数据,可以看到价格变成了10.99
4、删除数据
Button deleteButton = (Button) findViewById(R.id.delete_data);
deleteButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sqliteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book","pages > ?",new String[] { "500" });
}
});
删除数据,可以看到name数据The Lost Symbol被删除,其他项也被删除:
5、遍历数据
Button queryButton = (Button) findViewById(R.id.query_data);
queryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sqliteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book",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"));
Log.d("MainActivity","book name is " + name);
Log.d("MainActivity","book author is " + author);
Log.d("MainActivity","book pages is " + pages);
Log.d("MainActivity","book price is " + price);
} while (cursor.moveToNext());
}
cursor.close();
}
});
设置断点,debug模式下,单击query按键,运行到断点处,可以在watch窗口看到各个变量的数据:
6、代替数据
Button replaceData = (Button) findViewById(R.id.replace_data);
replaceData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sqliteDatabase db = dbHelper.getWritableDatabase();
db.beginTransaction();
try {
db.delete("Book",null);
// if (true) {
// throw new NullPointerException();
// }
ContentValues values = new ContentValues();
values.put("name","Game of Thrones");
values.put("author","George Martin");
values.put("pages",720);
values.put("price",20.85);
db.insert("Book",values);
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
db.endTransaction();
}
}
});
可以看到name数据被代替: