2

Need to delete records from sqlite db. One record row at a time and/or all records with a particular string value. I read many posts and most seem to be for Android and very different from plain Java - didn't find what I need.

Here's the code snippet I'm trying (delString comes in from a textfield), I did not set up record ID's.:

    Class.forName("org.sqlite.JDBC");
    Connection conn = DriverManager.getConnection("jdbc:sqlite:dufuss.db");

    delIt = Dialog_RecToDelete.delString; 
    String sql = "DELETE * FROM " + TABLE_NAME + " WHERE docName = ?";
    PreparedStatement preparedStatement = conn.prepareStatement(sql);
    preparedStatement.setString(1, delIt);
    ResultSet rs = preparedStatement.executeQuery();

    while (rs.next()) {
      // iterate through results
    }
  rs.close();
  conn.close();
2
  • 2
    use PreparedStatement#executeUpdate Commented Jun 18, 2013 at 21:51
  • 1
    did not work - tried it before posting. Get message: near "*": syntax error Commented Jun 18, 2013 at 21:57

2 Answers 2

5

According to the documentation, it should just be DELETE FROM ..., so remove the * in DELETE * FROM ....

Also, you should use PreparedStatement#executeUpdate instead of PreparedStatement#executeQuery

Sign up to request clarification or add additional context in comments.

1 Comment

Pefect! - it works! (deletes the row whose first column contain the delString value - just as I needed).
2

SQLite is light also on the syntax, they do not use the * as it's unnecessary (Who only deletes a column only and not the entire row?).

So, the following syntax should work:

"DELETE FROM " + TABLE_NAME + " WHERE docName = ?"

Also note, as @damo commented, that you need to use executeUpdate() instead of executeQuery() and possibly a commit is in order if it's not auto-commiting.

More info on DELETE syntax here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.