gpt4 book ai didi

安卓 SQLite : Failing to update records

转载 作者:行者123 更新时间:2023-11-29 01:53:29 24 4
gpt4 key购买 nike

我正在开发一个小型应用程序,用于记录地点及其所属的类别。现在,我只处理类别表,一旦我成功执行了所有 CRUD 操作,我就会继续处理应用程序的其余部分。

问题是,我设法插入、删除和读取该表,但我无法更新。我有这段代码:

数据库适配器:

...
public boolean updateCategory(long rowId, String name) {
ContentValues args = new ContentValues();
args.put(KEY_ID_CAT, rowId);
args.put(KEY_NAME_CAT, name);
return db.update(TABLE_CATEGORY, args, KEY_ROWID_CAT + "=" + rowId, null) > 0;
}
...

我的 EditCategory 类:

public class S7_EditCategory extends Activity {

EditText et_name_category;
private Context ctx;
int duration = Toast.LENGTH_SHORT;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.s7_edit_categories);

et_name_category = (EditText) findViewById(R.id.edit_category);
ctx = this;

final String nome = getIntent().getStringExtra("nome");
final long id_cat = getIntent().getIntExtra("_id", 0);

et_nome_category.setText(nome);

Button button_save = (Button) findViewById(R.id.button3);

button_save.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

try {

String new_cat = et_name_category.getText().toString();
DBAdapter dbadap = DBAdapter.getInstance(ctx);
dbadap.updateCategory(id_cat, new_cat);

Intent i = new Intent(ctx, S5_Categories.class);
setResult(RESULT_OK, i);
finish();

}

catch (Exception ex) {

throw new Error("some error");
}
}
});

}
}

Logcat 告诉我 ( _id = 0 ) 所以,我认为它与从上一个 Activity Category 传递的参数有关:

public class S5_Categories extends ListActivity {

private Context ctx;
private SimpleCursorAdapter adapter;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.s5_categorias);
ctx = this;

fillList();
getListView().setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parentView, View view, int position, long id) {

Cursor c = (Cursor) parentView.getItemAtPosition(position);

Intent i = new Intent(ctx, S7_EditCategoria.class);
i.putExtra("nome", c.getString(c.getColumnIndexOrThrow("nome")));
i.putExtra("id_cat", c.getColumnIndex("_id") + 1);
startActivityForResult(i, 0);

}
});
}

private void fillList() {
try {

DBAdapter dbadap = DBAdapter.getInstance(ctx);
Cursor c = dbadap.getAllCategorias();

String[] from = new String[] { "nome" };
int[] to = new int[] { android.R.id.text1 };

adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, c, from, to,
SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setListAdapter(adapter);
}

catch (Exception ex) {

throw new Error(ex.getMessage());
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch(requestCode) {

case 0:
if (data != null) {

DBAdapter dbadap = DBAdapter.getInstance(ctx);
Cursor cursor = dbadap.getAllCategorias();
adapter.changeCursor(cursor);

}
}
}
}

好吧,有人可以帮我吗?提前致谢恰帕


好的,现在我设法获得了那个参数 id,但是我在更新时仍然遇到错误:当我尝试时,我得到这样的错误:

使用 UPDATE category SET name=?, id_cat=? 更新 name=hospitals id_cat=4 时出错其中 id=4

请帮忙

恰帕

最佳答案

试试下面的方法

db.update(TABLE_CATEGORY, args, KEY_ROWID_CAT + "=?", new String[] { (rowId) });

代替

db.update(TABLE_CATEGORY, args, KEY_ROWID_CAT + "=" + rowId, null)

为什么要将它作为 int。他们有什么正当理由。我没有从您提供的代码中找到任何原因。如果他们没有正当理由意味着改变你的方法,如下所示。

public boolean updateCategory(long rowId, String name) {
ContentValues args = new ContentValues();
args.put(KEY_ID_CAT, rowId);
args.put(KEY_NAME_CAT, name);
db.update(TABLE_CATEGORY, args, KEY_ROWID_CAT + "=?", new String[] { (rowId) });
}

希望对您有所帮助。

关于安卓 SQLite : Failing to update records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16634159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com