Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite autoincrement - How to insert values?

I generate a SQLite table (in java):

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

afterwards I try to add rows using the INSERT comand:

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

i get the error:

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

I thought the row id would be generated automatically, but it seems that I miss anything.


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(); 

as result I received a null pointer exception at "prep.setInt(1,n);"

Do you see the fault?

like image 779
Anthea Avatar asked Nov 24 '11 00:11

Anthea


People also ask

How do I set Autoincrement value?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name. The name of the table whose AUTO_INCREMENT value you wish to change.

How manually insert data in SQLite database?

If you want to inset the data manually(fully graphical) do the following: Go to the DDMS perspective. File explorer (tab-menu) Locate your db (/data/data/com.

When you insert a new row into a SQLite database it automatically generates a value for?

If you don't specify the rowid value or you use a NULL value when you insert a new row, SQLite automatically assigns the next sequential integer, which is one larger than the largest rowid in the table. The rowid value starts at 1.


1 Answers

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"); 
like image 103
seth Avatar answered Sep 30 '22 14:09

seth