此文为例子
添加 例子
Cursor cursor = db.rawQuery("select * from student where name = ?",
new String[] { student.getName() });
//判断如果游标可以向下移动,代表该数据存在
if (cursor.moveToNext()) {
// 代表有这个
return;
}
ContentValues values = new ContentValues();
values.put("name", student.getName());
values.put("position", student.getPosition());
values.put("age", student.getAge());
db.insert("student", null, values);
db.close();
}
修改
public void update(String oldName,String newName,int age){
SQLiteDatabase db= mySqliteOpenHelper.getWritableDatabase();
db.execSQL("update student set name =?,age =? where name = ?",new Object[]{newName,age,oldName});
db.close();
}
删除
public void delete(String name){
SQLiteDatabase db= mySqliteOpenHelper.getWritableDatabase();
db.execSQL("delete from student where name = ?",new String[]{name});
db.close();
}
查询
*/
public ArrayList<Product> select(String type, String like) {
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
Cursor cursor = null;
// 普通的查询
if (type == null && like == null) {
cursor = db.rawQuery("select * from product", null);
} else if (type != null && like == null) {
cursor = db.query("product", null, null, null, null, null, "price "+ type);
} else if (type == null && like != null) {
cursor = db.rawQuery("select * from product where name like ?",new String[] { "%" + like + "%" });
} else if (type != null && like != null) {
cursor = db.query("product", null, "name like ?",new String[] { "%" + like + "%" }, null, null, "price "+ type);
}
productList.clear();
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String des = cursor.getString(cursor.getColumnIndex("des"));
int picture = cursor.getInt(cursor.getColumnIndex("picture"));
int price = cursor.getInt(cursor.getColumnIndex("price"));
productList.add(new Product(name, picture, price, des));
}
return productList;
}