2017-02-03 61 views
0

我嘗試更新從表中的一行工作,但它似乎犯規更新 (我還沒有錯誤,但其未更新)更新不是在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)?

回答

0

我不能肯定這一切設置正確,但如果你想修改數據,您將需要調用​​而不是getReadableDatabase說:

https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase()

+0

我試過了,但還是不行! –

+0

在日誌中,您是否看到異常/堆棧跟蹤或任何看似相關的警告消息? –

+0

E/SpannableStringBuilder:SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有一個零長度...我不知道這是有關 –

0

原文:

嘗試:

SQLiteDatabase database=this.getWriteableDatabase(); 

編輯:

我想你的DB代碼用一個簡單的活動......你的更新功能可按爲我工作得很好:

NotesDatabase的onCreat():

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("create table Notes (ID INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,detail TEXT)"); 
    db.close(); 
    insertData("Title1" ,"Detail1"); 
} 

活動:

public class UpdateShowEditNote extends AppCompatActivity { 

TextView title,detail; 

final NotesDataBase db=new NotesDataBase(this); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_update_show_edit_note); 
    title= (TextView) findViewById(R.id.title1); 
    detail=(TextView)findViewById(R.id.detail1); 
    Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
       db.updateData("1", "Title2", "Detail2"); 

       List<NotesData> data = db.getAllRecord(); 
       title.setText(data.get(0).getTitle()); 
       detail.setText(data.get(0).getDetail()); 
     } 
    }); 

    List<NotesData> data = db.getAllRecord(); 

    title.setText(data.get(0).getTitle()); 
    detail.setText(data.get(0).getDetail()); 
} 
} 

所以我猜你的問題是不是更新電話。

對應於此Answer您可能不會更改您的PRIMARY_KEY,因此可能不會將ID放在contentValues中用於更新查詢。

+1

您應該解釋您的解決方案。 – Blackbam

+0

我改變它,沒有工作 –

+0

getReadableDatabase只授予讀取訪問 - >你不能改變數據存儲 嗯這是我看到的唯一的事情,至極可負責您的問題,您可以發佈更多的代碼,也許 – fijili

0

新的答案:

我知道這不是回答你的問題,但我也不能破解這一個。我不知道爲什麼它沒有更新。我遇到同樣的問題。但是這個代碼有效。也許它對你有用。

MainActivity:

public class MainActivity extends AppCompatActivity { 

NotesDataBase myDb; 
EditText editID, editTITLE, editDETAIL; 
Button btnAddData; 
Button btnviewAll; 

Button btnviewUpdate; 

ArrayAdapter<String> adapter; 
ArrayList<String> itemList; 
ListView listV; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    myDb = new NotesDataBase(this); 

    editID = (EditText) findViewById(R.id.editID); 
    editTITLE = (EditText) findViewById(R.id.editTITLE); 
    editDETAIL = (EditText) findViewById(R.id.editDETAILS); 
    btnAddData = (Button) findViewById(R.id.InsertData); 
    btnviewAll = (Button) findViewById(R.id.getData); 
    btnviewUpdate = (Button) findViewById(R.id.updateButton); 

    String[] items = {""}; 
    itemList = new ArrayList<String>(Arrays.asList(items)); 
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.textView, itemList); 
    listV = (ListView) findViewById(R.id.list); 
    listV.setAdapter(adapter); 

    AddData(); 
    viewAll(); 
    UpdateData(); 
} 

public void UpdateData() { 
    btnviewUpdate.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        boolean isUpdate = myDb.updateData(editID.getText().toString(), 
          editTITLE.getText().toString(), 
          editDETAIL.getText().toString()); 
        if (isUpdate == true) 
         Toast.makeText(MainActivity.this, "Data Update", Toast.LENGTH_LONG).show(); 
        else 
         Toast.makeText(MainActivity.this, "Data not Updated", Toast.LENGTH_LONG).show(); 
       } 
      } 
    ); 
} 

public void AddData() { 
    btnAddData.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        boolean isInserted = myDb.insertData(editTITLE.getText().toString(), 
          editDETAIL.getText().toString()); 
        if (isInserted == true) 
         Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show(); 
        else 
         Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show(); 
       } 
      } 
    ); 
} 

public void viewAll() { 
    btnviewAll.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        itemList.clear(); 
        adapter.notifyDataSetChanged(); 

        Cursor c = myDb.getAllRecord(); 

        while (c.moveToNext()) { 
         String _id = c.getString(0); 
         String title = c.getString(1); 
         String details = c.getString(2); 
         adapter.add(_id + ": " + title + " " + details); 
         listV.setAdapter(adapter); 
         adapter.notifyDataSetChanged(); 
        } 

        myDb.close(); 
       } 
      } 
    ); 
} 

}

XML爲主要活動

<Button 
    android:text="Get data" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/getData" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:text="ID" 
    android:ems="10" 
    android:id="@+id/editID" 
    android:layout_below="@+id/getData" 
    android:layout_alignParentStart="true" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:text="TITLE" 
    android:ems="10" 
    android:id="@+id/editTITLE" 
    android:layout_below="@+id/editID" 
    android:layout_alignParentStart="true" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:text="DETAILS" 
    android:ems="10" 
    android:id="@+id/editDETAILS" 
    android:layout_below="@+id/editTITLE" 
    android:layout_alignParentStart="true" /> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/editDETAILS" 
    android:layout_alignParentStart="true" 
    android:id="@+id/list"/> 

<Button 
    android:text="Update data" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/updateButton" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentEnd="true" /> 

<Button 
    android:text="Insert data" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignEnd="@+id/editID" 
    android:id="@+id/InsertData" /> 

XML的名單:

<TextView 
    android:text="TextView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView" /> 

而且NotesDatabase的

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; 

} 

//I turned this into a cursor method, because I'm familiar with it 
public Cursor getAllRecord() { 
    SQLiteDatabase database = this.getReadableDatabase(); 
    Cursor res = database.rawQuery("select * from Notes", null); 
    return res; 
} 

}

舊的和錯誤的答案:(與校正錯字):

乍一看,我會說:去除圍繞注雙引號;

database.update(Notes,contentValues,"ID=?",new String[]{id}); 

讓我知道它是否有效。

+0

必須是雙cuotation兄弟,因爲它需要字符串 –

+0

我同意@fijili,這個問題似乎沒有被在你的更新電話中,如果我編輯的解決方案沒有幫助,你可以給我們一個關於wha的想法你想讓你的建築看起來像嗎? –