##建数据库
public class SqliteHelper extends SQLiteOpenHelper {
public SqliteHelper(Context context) {
super(context, "User.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(id integer primary key autoincrement," +
"name text," +
"img text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
##UserDao层
public class UserDao {
private final SQLiteDatabase db;
public UserDao(Context context){
SqliteHelper sqliteHelper = new SqliteHelper(context);
db = sqliteHelper.getWritableDatabase();
}
//添加
public void insert(String name,String img){
ContentValues values = new ContentValues();
values.put("name",name);
values.put("img",img);
db.insert("user", null, values);
}
//查询
public List<NewBean.NewData> query(){
Cursor query = db.query("user", null, null, null, null, null, null);
ArrayList<NewBean.NewData> list = new ArrayList<>();
while (query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
String img = query.getString(query.getColumnIndex("img"));
list.add(new NewBean.NewData(name,img));
}
return list;
}
//删除
public void delete(String name){
db.delete("user","name=?",new String[]{name});
}
}
数据库
最新推荐文章于 2024-07-31 17:36:10 发布