public class StudentDatabase extends SQLiteOpenHelper {
public StudentDatabase(@Nullable Context context) {
super(context, "student.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase pSQLiteDatabase) {
pSQLiteDatabase.execSQL("create table student(id int(10),name varchar(20),className varchar(60),major varchar(60),score int(20))");
}
//插入数据
public void insert(StudentBean pBean){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("id", pBean.getId());
values.put("name", pBean.getName());
values.put("className", pBean.getClassName());
values.put("major", pBean.getMajor());
values.put("score", pBean.getScore());
db.insert("student", null, values);
}
//删除数据
public void delete(StudentBean stu){
SQLiteDatabase db = getWritableDatabase();
db.delete("student", "id=?", new String[]{stu.getId()+""});
}
//删除表
public void deleteTable(SQLiteDatabase db){
db.execSQL("delete from info");
}
//修改数据
public void update(String oldName,StudentBean pBean){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("id", pBean.getId());
values.put("name", pBean.getName());
values.put("className", pBean.getClassName());
values.put("major", pBean.getMajor());
values.put("score", pBean.getScore());
db.update("student", values, "name=?", new String[]{oldName});
}
//查询数据
public List<StudentBean> select(){
ArrayList<StudentBean> beans = new ArrayList<>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.query("student", null, null, null, null, null, null);
while (cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String className = cursor.getString(cursor.getColumnIndex("className"));
String major = cursor.getString(cursor.getColumnIndex("major"));
int score = cursor.getInt(cursor.getColumnIndex("score"));
beans.add(new StudentBean(id, name, className, major, score));
}
return beans;
}
@Override
public void onUpgrade(SQLiteDatabase pSQLiteDatabase, int pI, int pI1) {
}
}
添加、查询比较简单,直接调用添加方法就行
删除:
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> pAdapterView, View pView, int pI, long pL) {
mDatabase.delete(mDatabase.select().get(pI));
mAdapter.delete(pI);
Toast.makeText(HomeActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
return true;
}
});
适配器删除:
public void delete(int pos){
mBeans.remove(pos);
notifyDataSetChanged();
}
修改:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> pAdapterView, View pView, int pI, long pL) {
pos = pI;
Intent intent = new Intent(HomeActivity.this,UpdateActivity.class);
//点击的条目内容传到修改页面,修改页面修改完之后把修改的数据传过来就行
intent.putExtra("data", mDatabase.select().get(pI));
startActivityForResult(intent,UPDATE_TYPE);
}
});
if(requestCode == UPDATE_TYPE && resultCode == 300){
//获取修改完的数据
StudentBean bean = (StudentBean) data.getSerializableExtra("bean");
StudentBean studentBean = mDatabase.select().get(pos);
List<StudentBean> select = mDatabase.select();
select.set(pos, bean);
mAdapter.loadBeans(select);
mDatabase.update(studentBean.getId()+"", bean);
mDatabase.update(studentBean.getName(), bean);
mDatabase.update(studentBean.getClassName(), bean);
mDatabase.update(studentBean.getMajor(), bean);
mDatabase.update(studentBean.getScore()+"", bean);
}
//删除表
InfoDatabase database = newd InfoDatabase(this);
SQLiteDatabase writableDatabase = database .getWritableDatabase();
database .delete(writableDatabase);