2
最近我一直有一个奇怪的问题,关于我的数据库驱动的Android应用程序。我之前使用代码更新没有问题,但现在它不会允许我更新对记录的更改。Android的Sqlite更新不起作用
我确实尝试记录,如果它保存我所做的更改,但它不。
下面是我使用的代码:
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();
}
我真的不知道什么是错的,有人在这里已经经历过这个?不妨帮我弄清楚我在代码中缺少的东西。非常感谢帮助。谢谢。
那么究竟什么是错的? – Szymon
我所做的更改不会更新。我确实在日志中打印了结果,但它说我的更改没有更新。 – Dunkey
似乎您希望在底层数据更改时更新游标行。事实并非如此,您必须重新查询以查看数据库更改。 – laalto