简体   繁体   English

无法将数据插入Sqlite数据库

[英]Can't insert data into Sqlite database

This is the code for me to insert data into the database. 这是我将数据插入数据库的代码。 Everything is fine with no errors. 一切都很好,没有错误。 I typed everything that I need supposedly. 我输入了我应该输入的所有内容。

But somehow I am still not able to insert the data. 但是我仍然无法以某种方式插入数据。 What is the reason? 是什么原因? When I check the database, the table cart is empty. 当我检查数据库时,表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 + "');");
    }
}

What are you trying to do? 你想做什么?
First you find the row with id_ = rowId and then if this row exists you want to insert another row with the same id_ ? 首先,找到具有id_ = rowId的行,然后如果该行存在, id_ = rowId插入另一个具有相同id_行?
Isn't this id_ UNIQUE in the table? 这个id_ UNIQUE在表中?
Maybe you should do the opposite, meaning if this id_ does not exist then insert the new row with some value for cartname : 也许您应该做相反的事情,这意味着如果此id_不存在,则插入具有cartname值的新行:

if (myCursor.getCount() == 0) {
    mydb2.execSQL("insert into cart(id_, cartRecord) VALUES(" + rowId + ", '" + cartname + "');");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM