2018夏季實訓之有關數據庫基本操作的函數
添加
final Person p2 = new Person();
Button bt=findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string=((EditText)findViewById(R.id.et1)).getText().toString();
p2.setName(string);
p2.save(new SaveListener<String>() {
@Override
public void done(String objectId, BmobException e) {
if(e==null){
Toast.makeText(getApplicationContext(),"添加數據成功,返回objectId為:"+objectId,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"創建數據失敗:" + e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
}
});
查詢
Button bt2=findViewById(R.id.bt2);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string1=((EditText)findViewById(R.id.et2)).getText().toString();
BmobQuery<Person> bmobQuery = new BmobQuery<Person>();
bmobQuery.getObject(string1, new QueryListener<Person>() {
@Override
public void done(Person object, BmobException e) {
if(e==null){
Toast.makeText(getApplicationContext(),"查詢成功",Toast.LENGTH_SHORT).show();
String st=object.getName();
TextView tv=findViewById(R.id.tv);
tv.setText(st);
}else{
Toast.makeText(getApplicationContext(),"查詢失敗:" + e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
}
});
依次查找
String sql = "Select *from MyAddBook";
new BmobQuery<Person>().doSQLQuery(sql, new SQLQueryListener<Person>() {
@Override
public void done(BmobQueryResult<Person> bmobQueryResult, BmobException e) {
if (e == null) {
List<Person> list = (List<Person>) bmobQueryResult.getResults();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Person book = list.get(i);
}
} else {
}
}
}
});
判斷用戶名密碼是否正確
final String username="";
final String password="";
String sql = "Select *from Person";
new BmobQuery<Person>().doSQLQuery(sql, new SQLQueryListener<Person>() {
@Override
public void done(BmobQueryResult<Person> bmobQueryResult, BmobException e) {
if (e == null) {
List<Person> list = (List<Person>) bmobQueryResult.getResults();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Person person = list.get(i);
if(person.getUsername()==username&&person.getPassword()==password){ //可以先比較用戶名,找到後再比對密碼
//成功登錄,為person類,跳轉。
}
}
//顯示用戶名或密碼錯誤。
}
}
}
});
刪除
Button bt3=findViewById(R.id.bt3);
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string2=((EditText)findViewById(R.id.et3)).getText().toString();
p2.setObjectId(string2);
p2.delete(new UpdateListener() {
@Override
public void done(BmobException e) {
if(e==null){
Toast.makeText(getApplicationContext(),"刪除成功:"+p2.getUpdatedAt(),Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"刪除失敗:" + e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
修改
Person p2 = new Person();
p2.setAddress("北京朝陽");
p2.update("6b6c11c537", new UpdateListener() {
@Override
public void done(BmobException e) {
if(e==null){
toast("更新成功:"+p2.getUpdatedAt());
}else{
toast("更新失敗:" + e.getMessage());
}
}
});
2018夏季實訓之有關數據庫基本操作的函數