samedi 30 avril 2016

Android: how do I increment SQLite INTEGER PRIMARY KEY AUTOINCREMENT Column?

In my activity I have a Save button that upon clicking will save a bunch of user input data to a SQLite table. On the same click, I want to also increment an integer counter in another table. The counter is set up as "cardnum".

What am I missing here?

Activity file:

...
public void onClickSave(View v) {
    int stringToDo = cListenerEditText.getText().toString().replace(" ", "").length();
    int stringDueDate = fListenerEditText.getText().toString().replace(" ", "").length();
    int stringDueTime = gListenerEditText.getText().toString().replace(" ", "").length();

String todo = cListenerEditText.getText().toString();
String note1 = dListenerEditText.getText().toString();
String note2 = eListenerEditText.getText().toString();
String duedate =  fListenerEditText.getText().toString();
String duetime =  gListenerEditText.getText().toString();
long timestamp = System.currentTimeMillis();

// Increment the counter column in the Count Table.
int cardnum = ++ cardnum;

helper = new DatabaseHelper(CardViewActivity.this);

helper.insertIntoTableUserInputs(todo,note1,note2,duedate,duetime,timestamp);
helper.insertIntoTableCount(cardnum);

startActivity(new Intent(CardViewActivity.this,ListActivity.class));
}     

DBHelper file:

...
private static final String SQL_CREATE_TABLE =
        "CREATE TABLE IF NOT EXISTS " + DBContract.DBEntry.TABLE_NAME_COUNT +
            "(" + DBContract.DBEntry.COLUMN_NAME_CARDNUM +
                  " INTEGER PRIMARY KEY AUTOINCREMENT" + ")";

public void insertIntoTableCount(int cardnum) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values2 = new ContentValues();
    values2.put(DBContract.DBEntry.COLUMN_NAME_CARDNUM, cardnum);

    db.insert(DBContract.DBEntry.TABLE_NAME_COUNT, null, values2);

    db.close();
}

Aucun commentaire:

Enregistrer un commentaire