Search code examples
javaandroidsqliteauto-increment

Letting Android choose the next SQLITE _ID


Somewhere in my code I do this:

        contentValues = new ContentValues();

//      contentValues.put("_ID",BaseColumns._ID); // Not Working can someone explain what this is and how it's used BaseColumns._ID ?
        contentValues.put("_ID", null); // Not working, Everywhere they say to pass in a null value and Android will do it's magic ... 

        contentValues.put("_ID", "1")    // This works but has to be changed manually every time
        contentValues.put("login", username.getText().toString());
        contentValues.put("password", pwd.getText().toString());
        contentValues.put("type", type);

This is my small schema:

public static final String CREATE_DATABASE = "CREATE TABLE "+
            TABLE +"(_ID INTEGER PRIMARY KEY AUTOINCREMENT, login VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, type CHAR(1) NOT NULL)";

Some people say not to put AUTOINCREMENT, but in some other websites you can see it in the code. I'm not sure what to do anymore. How do I get Android to choose the next increment value ?


Solution

  • Using autoincrement will auto assign an ID to any new row inserted into the database

    You do not need to call contentValues.put("_ID", null); or even access the column in any way when you insert something into the database, it is done automatically