lundi 8 juin 2015

Insert into table with autoincrement primary key

I'm creating a table as follows:

@Override
public void onCreate(SQLiteDatabase db) {
    String sql =
            "CREATE TABLE IF NOT EXISTS users (" +
                    "_id INTEGER PRIMARY KEY, " +
                    "name TEXT NOT NULL, " +
                    "password TEXT NOT NULL);";
    db.execSQL(sql);

    sql = "INSERT INTO users VALUES ('testuser', 'textpassword')";
    db.execSQL(sql);
}

But when I try to insert a user, I'm getting the following exception:

android.database.sqlite.SQLiteException: table users has 3 columns but 2 values were supplied (code 1): , while compiling: INSERT INTO users VALUES ('testuser', 'textpassword')

As _id is the primarykey, why is it expecting it? I also tried AUTOINCREMENT but it doesn't work.

Aucun commentaire:

Enregistrer un commentaire