samedi 27 février 2016

Using sqlite db.replace without putting unique field in the replace contentvalues

First off. Is it possible to not pass the unique value in the contentvalues for a db.replace command and have it update an existing row?

It seems like you have to trigger a unique constraint in order for the update to work, otherwise the command executes an insert.

Why I'm so confused is because this was working for me (update) with db.replace and I was NOT sending the unique key in the contentvalues. Am I completely wrong here? The reason I know I was never sending it, is because I still don't even know how to even get that value from the db.

Here is my code (create NEW record

        //Create the new tab record
        tabsValues.put(Contract.TabsTable.COLUMN_REFERENCE_ID, eTextCardDetails.getText().toString());
        tabsValues.put(Contract.TabsTable.COLUMN_TAB_CLOSED, 0);
        long newTabId = db.insert(Contract.TabsTable.TABLE_NAME, null, tabsValues); //Insert the records
        eTextOrderTabId.setText(String.valueOf(newTabId)); //Update the UI with the new Tab ID#

        //Create a new map of values for tablines
        cursor.moveToFirst();
        for(int i = 0; i < cursor.getCount(); i++ )
        {
            if(Integer.parseInt(editTextCount[i].getText().toString()) > 0)
            {
                int itemId = editTextCount[i].getId();
                int itemCount = Integer.parseInt(editTextCount[i].getText().toString());
                tabsLinesValues.put(Contract.TabsLinesTable.COLUMN_TAB_ID, newTabId); //Save the itemId in the DB
                tabsLinesValues.put(Contract.TabsLinesTable.COLUMN_ITEM_ID, itemId); //Save the itemId in the DB
                tabsLinesValues.put(Contract.TabsLinesTable.COLUMN_ITEM_COUNT, itemCount); //Save the itemCount in the DB
                //Insert the item details for the order in the db
                long newTabsLinesRow = db.insert(Contract.TabsLinesTable.TABLE_NAME, null, tabsLinesValues); //Insert the records
                Toast.makeText(this, "tabsLineId: " + newTabsLinesRow + " for itemId: " + itemId, Toast.LENGTH_SHORT).show();
                tabsLinesValues.clear();
            }
            cursor.moveToNext();
        }
        Toast.makeText(this, "Tab #" +newTabId+ " Started!", Toast.LENGTH_SHORT).show();
        cursor.close();
        finish();
    }catch (Exception erMsg)
    {
        erMsg.printStackTrace();
    }
}

and this is working fine. It creates the new rows as expected. Here is my update method that was updating already existing records and inserting new records. Unfortunately now, it ALWAYS inserts new rows.

I think I understand that it is because there is no unique constraint on my save, so it inserts new rows. The thing is that I never was sending the unique key before and it was updating previous records and inserting new ones that didn't exist previously. If I don't send the unique key, how can you perform an update?

I originally was using a db.update method but that only updated previous rows and I needed to add new ones that didn't exist. I came across db.update and it was working fine, now I can't get it to work.

public void saveTab(View view)
{
    int tabId = Integer.parseInt(eTextOrderTabId.getText().toString());
    String[] argsItemId = {String.valueOf(tabId)};
    cursor.moveToFirst();
    try
    {
        ContentValues updateTabValues = new ContentValues(); //Create the new content values object
        //Create a new map of values for tablines
        for(int i = 0; i < cursor.getCount(); i++ )
        {
            if(Integer.parseInt(editTextCount[i].getText().toString()) > 0)
            {
                int itemId = editTextCount[i].getId();
                int itemCount = Integer.parseInt(editTextCount[i].getText().toString());
                updateTabValues.put(Contract.TabsLinesTable.COLUMN_TAB_ID, tabId);
                updateTabValues.put(Contract.TabsLinesTable.COLUMN_ITEM_ID, itemId);
                updateTabValues.put(Contract.TabsLinesTable.COLUMN_ITEM_COUNT, itemCount);
                //Update the item row details for each item in the tab
                db.replace(Contract.TabsLinesTable.TABLE_NAME, null, updateTabValues); //Insert the records
                updateTabValues.clear();
            }
        }
        Toast.makeText(this, "Tab #" +tabId+ " Saved!", Toast.LENGTH_SHORT).show();
        finish();
    }catch (Exception erMsg)
    {
        erMsg.printStackTrace();
    }
}

Aucun commentaire:

Enregistrer un commentaire