[英]Can't insert data into Sqlite database
這是我將數據插入數據庫的代碼。 一切都很好,沒有錯誤。 我輸入了我應該輸入的所有內容。
但是我仍然無法以某種方式插入數據。 是什么原因? 當我檢查數據庫時,表cart
是空的。
public void insertrecord() {
Bundle bundle = this.getIntent().getExtras();
long rowId = bundle.getLong("rowId");
mydb2 = dbhelper.getWritableDatabase();
myCursor2 = mydb2.query(Dbhelper.CART_NAME, allColumns2, "ID_=" + String.valueOf(rowId), null, null, null, null);
myCursor.moveToFirst();
if (!myCursor.isAfterLast()) {
String cartname = (myCursor.getString(myCursor.getColumnIndex(Dbhelper.COLUMN_TITLE)));
mydb2.execSQL("insert into cart(id_, cartRecord) VALUES(" + rowId + ", '" + cartname + "');");
}
}
你想做什么?
首先,找到具有id_ = rowId
的行,然后如果該行存在, id_ = rowId
插入另一個具有相同id_
行?
這個id_
UNIQUE
在表中?
也許您應該做相反的事情,這意味着如果此id_
不存在,則插入具有cartname
值的新行:
if (myCursor.getCount() == 0) {
mydb2.execSQL("insert into cart(id_, cartRecord) VALUES(" + rowId + ", '" + cartname + "');");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.