mardi 15 décembre 2015

Java SQLite : exploiting a ResultSet for a COUNT query

I'm using SQLite in a Java application, and I need to know if UNIQUE constraints may be violated before I add a row.

Therefore I wrote a small method that counts the number of rows with the same couple of unique values (here it is a method with the for my table User, in the class that creates the Database ; I add users quite often) :

public boolean testUserId(String a) {
    try {
        String query = "SELECT COUNT(*) FROM USER WHERE ID = " + a + ")";

        ResultSet rs = select(query);

        if (rs.next()) {
            System.out.println("Point 1");
            boolean ok = (rs.getInt(1) == 0);

            if (ok) {
                return true;
            }
        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    return false;
}

But it never goes to Point 1. Can anybody familiar with this tell me where I'm wrong ? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire