Hi,

I am writing a wrapper around JDBC and I am having difficulties with
the isBeforeFirst() and isFirst() methods in the resultSet. In two
words, isBeforeFirst() always return false and the first next()
operation sets the isFirst() to false but does not move the cursor =>
I get twice the first entry.

Here is a sample code which demonstrates the problem.
The test database can be found at http://www.pysquared.com/files/Java/
JavaSQLiteExample/pmp.db.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Very Basic SQLite Database Example
 *
 * @author Brandon Tanner (then revised by Laurent Ferier)
 */
public class SQLiteTest {

        private static void Assert(boolean val) {
                if (!val)
                        throw new RuntimeException("Assertion failed");
        }

        public static void main(final String[] args) throws SQLException,
                        ClassNotFoundException {
                Class.forName("org.sqlite.JDBC");
                Connection connection = DriverManager.getConnection(
                                "jdbc:sqlite:pmp.db", null, null);
                PreparedStatement stmt = connection
                                .prepareStatement("SELECT * FROM 
pmp_countries;");
                ResultSet rs = stmt.executeQuery();

                // Step 1
                // These two asserts might be ok. It would mean that the result 
set
                // provided is positioned on the first element
                Assert(rs.isFirst() == true);
                Assert(rs.isBeforeFirst() == false);

                String first = rs.getString("country_name");

                rs.next();
                // Step 2
                // These two asserts might also be ok, since, considering the 
Step 1
                // we should be on the second element of the result set
                Assert(rs.isFirst() == false);
                Assert(rs.isBeforeFirst() == false);
                String second = rs.getString("country_name");

                // But in this case, this is WRONG since there are no 
duplicates in
the database
                Assert(first.equals(second));

                // After this initial problem, the next entries are correctly 
read
                rs.next();
                Assert(!(first.equals(rs.getString("country_name"))));
                Assert(!(second.equals(rs.getString("country_name"))));

                connection.close();

                // I suspect that the correct behaviour would be to return
                // in Step 1 :
                //   true  for rs.isBeforeFirst()
                //   false for rs.isFirst()
                //
                // and in Step 2 :
                //   false for rs.isBeforeFirst()
                //   true  for rs.isFirst()
        }
}

Is this problem known ?
Do you plan to have a fix for this ?


Thank you for all the great work you have already done.
Regards
Laurent FERIER


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLiteJDBC" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlitejdbc?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to