Android Sqlite更新不起作用

时间:2013-10-05 09:01:49

标签: android sqlite

我最近在使用数据库驱动的Android应用程序时遇到了一个奇怪的问题。我之前使用代码UPDATE没有问题,但现在它不允许我更新我的记录更改。

我确实尝试记录是否保存了我所做的更改,但事实并非如此。

这是我正在使用的代码:

private void initControls() {

    userInput = (EditText) findViewById (R.id.editTextDialogUserInput);

    save = (Button) findViewById (R.id.btSave);
    cancel = (Button) findViewById (R.id.btCancel);
    save.setOnClickListener(this);
    cancel.setOnClickListener(this);

    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        stID = extras.getString("dog_id");
        dog_name = extras.getString("dog_name");
        cursor = dbHelper.fetchbBreedByName(dog_name);

        strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
        Log.d("Animal ID", "Animal ID is " + stID + " and breed is " + dog_name);
        userInput.setText(strDesc);
    }

}

private void checkDatabaseConnection() {
    // TODO Auto-generated method stub
    dbHelper = new DBHelper(this);

    try {
            dbHelper.createDataBase();
    } catch (IOException ioe) {
            throw new Error("Unable to create database");
    }

    try {
            dbHelper.openDataBase();
    } catch (SQLException sqle) {
            throw sqle;
    }

   }

@Override
public void onClick(View v) {

    switch(v.getId()){
    case R.id.btSave:
        if(userInput.equals("")){
            Toast.makeText(this, "No input", Toast.LENGTH_SHORT).show();    
        }
        else {
            id = Long.valueOf(stID);
            dbHelper.updateDescription( id, userInput.getText().toString() );
            Toast.makeText(this, "Description has been updated successfully!",
                    Toast.LENGTH_SHORT).show();
            strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
            Log.d("Updated", dog_name + " " + strDesc);
            Intent i = new Intent(this, DogClass.class);
            startActivity(i);
            finish();
        }
        break;
    case R.id.btCancel:
        userInput.setText("");
        break;
    }
}

@Override
protected void onDestroy() {
    dbHelper.close(); // close DB
    cursor.close(); // close cursor
    super.onDestroy();
}

Whole code can be viewed here

我真的不知道出了什么问题,这里有人经历过这个吗?不妨帮我弄清楚我的代码中缺少的东西。非常感谢帮助。感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用像这样的更新方法..

 SQLiteDatabase database = dbHelper.getWritableDatabase();
 ContentValues cv = new ContentValues();
 cv.put(IConstants.FOLDER_LOCKED, isSelected);
 database.update(FOLDER_TABLE, cv, IConstants.FOLDER_NAME + "= ?",
 new String[] { foldername });