创建库的帮助类
public class MySqlit extends SQLiteOpenHelper { public MySqlit(Context context) { super(context,"User.db",null,1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table user(aid text,title text,description text)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
UserDao的增删改查
public class Userdao {
private Sqlist sqlist;
public Userdao(Context context) {
sqlist = new Sqlist(context);
}
public void add(String name,String price,String brand){
SQLiteDatabase database = sqlist.getReadableDatabase();
database.execSQL("insert into user(id,name,price,brand) values(null,?,?,?)",
new String[]{name,price,brand});
database.close();
}
public List
List
SQLiteDatabase database = sqlist.getReadableDatabase();
Cursor cursor = database.rawQuery("select * from user",
new String[]{});
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String price = cursor.getString(cursor.getColumnIndex("price"));
String brand = cursor.getString(cursor.getColumnIndex("brand"));
User user = new User(name, price, brand);
list.add(user);
}
return list;
}
public void delete(String name){
SQLiteDatabase database = sqlist.getReadableDatabase();
database.execSQL("delete from user where name=?", new String[]{name});
database.close();
}
public void update(String tj,String newname,String newprice,String newbrand){
SQLiteDatabase database = sqlist.getReadableDatabase();
database.execSQL("update user set name=?,price=?,brand=? where name=?",
new String[]{newname,newprice,newbrand,tj});
database.close();
}
}