简体   繁体   中英

How to add rows to a table after the columns were altered? (SQLite, JAVA)

I am trying to add rows to a table. Initially, I had this code but it creates an error that there are 8 columns (additional columns were altered, initially I only had 2 columns) but I am only adding 2 values:

PreparedStatement addDate = 
con.prepareStatement("insert into contributions values(?,?)");

           addDate.setString(2, string);
           addDate.execute();
     }

And then I tried this:

  public void addDateToContributionsTable(String string) throws ClassNotFoundException, SQLException {
         if(con == null) {
             // get connection
             getConnection();
         }
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery("Select * from contributions");
          ResultSetMetaData rsmd = rs.getMetaData();
          int columnsNumber = rsmd.getColumnCount();
          StringBuilder sb = new StringBuilder();
          for(int i= 0; i < columnsNumber; i++){
              sb.append("?");
              sb.append(",");
          }

          System.out.println(sb.toString());
          System.out.println("insert into contributions values('"+sb.toString()+"')");
          PreparedStatement addDate = con
                    .prepareStatement("insert into contributions values('"+sb.toString()+"')");


          addDate.setString(2, string);
          addDate.execute();
     }

But I am still having the same error:

  [SQLITE_ERROR] SQL error or missing database (table contributions has 8 columns but 1 values were supplied)

Any help would be appreciated. :)

The values don't know where they should be inserted, so I suggest writing the column names for each value.

reparedStatement addDate = 
con.prepareStatement("insert into contributions (COLUMN_NAMES) values (?,?)");

       addDate.setString(2, string);
       addDate.execute();
 }

You added multiple bind parameters in the SQL, but you only ever bound one of them:

addDate.setString(2, string);

You need to call setXXX() per bind parameter.

If you are really sure you want to use the same value, you can instead use named parameters instead, then you won't have to call setXXX() multiple times. You can refer to this: Named parameters in JDBC

Also, questionMarksList and StringBuilder sb are doing the same thing over two loops.

Edit

If your SQL has 2 question marks, you have 2 bind parameters, then you need to set two bind parameters.

eg For SQL with 3 bind parameters:

INSERT INTO MYTABLE VALUES(?, ?, ?)

You need to provide 3 values:

addDate.setString(1, "String1"); // Bind to first question mark
addDate.setString(2, "String2"); // etc.
addDate.setString(3, "String3");

Thanks to all that responded to my question. The problem was with the number of ? I had on my statement every time I increase or decrease the column numbers. The only problem with my statement is having this expression '' . When I changed it to "+questionMarks+" instead of '"+questionMarks+"', it worked. Does not matter how many setXXX() method I use as long as the number of ? are the same with the number of columns on the table, it will work. Having the code below, I did not encounter any errors anymore.

  PreparedStatement addmembers = con
                    .prepareStatement("insert into membersdata values "+questionMarks+"");
                  addmembers.setString(2, name);
                  addmembers.setString(3, accounts);
                  addmembers.setString(4, email);
                  addmembers.execute();             

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM