public class UserSqliteHelper extends SQLiteOpenHelper{
public static String dbName = "Data.db";
public static int dbVersion = 1;
private SQLiteDatabase db;
public UserSqliteHelper(Context context) {
super(context, dbName, null, dbVersion);
// TODO Auto-generated constructor stub
db = getWritableDatabase();
}
public void insert(ContentValues cv){
db.insert("User", null, cv);
}
public void update(ContentValues cv,int id) {
db.update("User", cv, "_id="+id, null);
}
public void delete(int id) {
db.delete("User", "_id="+id, null);
}
public ArrayList<User> select(String where) {
String sql = "select * from User";
if (where != null) {
sql += where;
}
Cursor cursor = db.rawQuery(sql, null);
ArrayList<User> list = new ArrayList<User>();
while(cursor.moveToNext()){
User user = new User();
user.id = cursor.getInt(cursor.getColumnIndex("_id"));
user.name = cursor.getString(cursor.getColumnIndex("name"));
user.pwd = cursor.getString(cursor.getColumnIndex("pwd"));
list.add(user);
}
cursor.close();
return list;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists User(_id INTEGER primary key,name varchar(10),pwd varchar(50))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
if (newVersion>oldVersion) {
}
}
public void destory(){
if (db != null) {
db.close();
}
}
}
Android 数据库工具类
最新推荐文章于 2021-05-26 18:43:26 发布