Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: The column index is out of range: 1, number of columns: 0

I'm trying to solve the problem of doing an insert into a Postgresql table

I looked at this similar question but it did not solve my problem

ERROR : The column index is out of range: 1, number of columns: 0

here is the part of code getting the error:

String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES($1,$2,$3,$4)";

PreparedStatement prepareStatement = connection.prepareStatement(query);
prepareStatement.setInt(1, nbStar);
prepareStatement.setString(2, body);
prepareStatement.setString(3, author);
prepareStatement.setInt(4, productId);

boolean executed = prepareStatement.execute();

i tried several times to change the index number but still the same error

and here is the schema of the table:

table schema

can anyone give me an advice ?

thanks.

like image 835
Nesan Mano Avatar asked Apr 06 '16 00:04

Nesan Mano


1 Answers

In the sql query, you want to insert the values for 5 fields (id, nbstar, body, author, product_id) but there are only 4 values VALUES($1,$2,$3,$4).


Update following your edited question, just modify your query as follows:

VALUES($1,$2,$3,$4) 

to

VALUES(?,?,?,?)
like image 121
Tran Ho Avatar answered Sep 19 '22 18:09

Tran Ho