Search code examples
javasqlitesql-insertauto-increment

ROWID INTEGER PRIMARY KEY AUTOINCREMENT - How to insert values?


I created an SQLite table in Java:

create table participants (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, col1,col2);

I tried to add rows :

insert into participants values ("bla","blub");

Error:

java.sql.SQLException: table participants has 3 columns but 2 values were supplied

I thought ROWID would be generated automatically. I tried another solution:

PreparedStatement prep = conn.prepareStatement("insert into participants values (?,?,?);");
Integer n = null;
prep.setInt(1,n);
prep.setString(2, "bla");
prep.setString(3, "blub");
prep.addBatch();
prep.executeBatch();

I received a null pointer exception at prep.setInt(1,n);. Do you see the fault?


Solution

  • Have you tried indicating to which fields of the table the parameters you are passing are supposed to be related?

    INSERT INTO table_name (column1, column2, column3,...)
    VALUES (value1, value2, value3,...)
    

    In your case maybe something like:

    INSERT INTO participants(col1, col2) VALUES ("bla","blub");