我嘗試更新從表中的一行工作,但它似乎犯規更新 (我還沒有錯誤,但其未更新)更新不是在android系統
類NoteDatabase
public class NotesDataBase extends SQLiteOpenHelper{
public static final String DB_name ="dataNotes.db";
public NotesDataBase(Context context) {
super(context,DB_name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table Notes (ID INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,detail TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Notes");
}
public boolean insertData(String title,String detail){
SQLiteDatabase database=getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("title",title);
contentValues.put("detail",detail);
long result=database.insert("Notes",null,contentValues);
if (result==-1)
return false;
else return true;
}
public boolean updateData(String id,String title,String detail){
SQLiteDatabase database=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("ID",id);
contentValues.put("title",title);
contentValues.put("detail",detail);
database.update("Notes",contentValues,"ID=?",new String[]{id});
return true;
}
public boolean deleteData(String id){
SQLiteDatabase database=getWritableDatabase();
database.delete("Notes","ID=?",new String[]{id});
return true;
}
public ArrayList getAllRecord(){
ArrayList arrayList=new ArrayList<NotesData>();
SQLiteDatabase database=this.getReadableDatabase(); //the error is here
Cursor res=database.rawQuery("select * from Notes",null);
res.moveToFirst();
while (res.isAfterLast()==false){
String ID=res.getString(0);
String title=res.getString(1);
String detail=res.getString(2);
arrayList.add(new NotesData(ID,title,detail));
res.moveToNext();
}
return arrayList;
}
public NotesData getOneRow(String id){
SQLiteDatabase db=getReadableDatabase();
Cursor res=db.rawQuery("select * from Notes WHERE id = "+id,null);
if (res!=null)
res.moveToFirst();
NotesData notes=new NotesData(res.getString(res.getColumnIndex("ID"))
,res.getString(res.getColumnIndex("title"))
,res.getString(res.getColumnIndex("detail")));
return notes;
}
}
在下面的類IM使用更新方法:
public class UpdateShowEditNote extends AppCompatActivity {
EditText title,detail;
String myTitle ;
String myDetail;
String ID;
NotesDataBase db=new NotesDataBase(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_show_edit_note);
title= (EditText) findViewById(R.id.title1);
detail=(EditText)findViewById(R.id.detail1);
myTitle=getIntent().getStringExtra("title_key");
myDetail=getIntent().getStringExtra("detail_key");
ID=getIntent().getStringExtra("ID_key");
title.setText(myTitle); //get title and detail from listview
detail.setText(myDetail);
}
@Override
public void onBackPressed() {
if((myTitle.equals(title.getText().toString()))&&(myDetail.equals(detail.getText().toString()))) {
super.onBackPressed();
}
else
{
boolean res= db.updateData(ID,myTitle,myDetail);
if(res==true)
Toast.makeText(getBaseContext(),"the changed has been saved",Toast.LENGTH_LONG).show();
super.onBackPressed();
}
}
}
級NoteData
public class NotesData {
private String ID,title ,detail;
NotesData(){}
NotesData(String ID,String title ,String detail){
this.title=title;
this.detail=detail;
this.ID=ID;
}
NotesData(String title ,String detail){
this.title=title;
this.detail=detail;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
它如何工作(database.update)?
我試過了,但還是不行! –
在日誌中,您是否看到異常/堆棧跟蹤或任何看似相關的警告消息? –
E/SpannableStringBuilder:SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有一個零長度...我不知道這是有關 –