I wrote a program that populates a database with a list of words. Problem is, it throws up the \"Exception in thread \"main\" java.sql.SQLException: near \"s\": syntax error\" e
Use PreparedStatement to avoid problems with erroneous input:
PreparedStatement p = c.prepare("INSERT INTO WORDS (ID,CAT,WORD) VALUES(?, ?, ?)");
p.setInt(1, i);
p.setString(2, wordcat);
p.setString(3, word);
p.execute();
//commit results if using InnoDB, etc
Thanks Rogue for a good code. I had an error in SQLite during INSERT operation, SQL string contained "'" inside a text. So, an escaping in this way helped to me. I've changed a code a bit.
PreparedStatement prst = conn.prepareStatement(
"INSERT INTO test(id, name, type) "
+ "VALUES(?, ?, ?)");
prst.setLong(1, id);
prst.setString(2, title);
prst.setObject(3, type);
prst.execute();
So when I tried to insert a null value in INTEGER column, I used a method setObject().