Hi everybody,
I have a problem with the function DISTINCT. If a row is marked as deleted, in a special case the ResultSet is wrong.
Here is an example. First of all I create a new table. If it already exists, I delete the old one. Then I fill it with values. After that, I delete one entry.
Have a look at the comment after the DELETE-Statement (line 38/39)
---------------------------------------------------
import java.io.File;
import java.sql.*;
public class MinimalClass {
public static void main(String[] args) {
String pathToDBF = "c:/dbf";
String connectionUrl;
Connection con;
Statement stmt;
String sSQL;
connectionUrl = "jdbc:dbf:/" + pathToDBF;
try {
// Delete the dbf-File if necessary
File file = new File(pathToDBF + "/t1.dbf");
if (file.exists()) {
file.delete();
}
Class.forName("com.hxtt.sql.dbf.DBFDriver");
con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
// create a Table
sSQL = "CREATE TABLE IF NOT EXISTS t1 (key INT, val INT)";
stmt.executeUpdate(sSQL);
// fill with values
sSQL = "INSERT INTO t1 (key, val) VALUES (1,1)";
stmt.executeUpdate(sSQL);
sSQL = "INSERT INTO t1 (key, val) VALUES (2,2)";
stmt.executeUpdate(sSQL);
sSQL = "INSERT INTO t1 (key, val) VALUES (3,2)";
stmt.executeUpdate(sSQL);
// delete one value
sSQL = "DELETE FROM t1 WHERE key=3";
// change key=3 to key=2 in the line above and compare the result
stmt.executeUpdate(sSQL);
// Output
sSQL = "SELECT DISTINCT(val) AS myRow FROM t1";
ResultSet rs = stmt.executeQuery(sSQL);
while (rs.next()) {
System.out.println(rs.getInt("myRow"));
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------------------------------------------------
I know, that I can use "GROUP BY" instead of DISTINCT, but in other cases this might not work.
|
Sorry for the unformated java-Code. I hope you can read it anyway :-) Is there a possibility to edit my posting?
|
Checked. It output:
1
2
It's normal. What's your wrong?
|
If I use
DELETE FROM t1 WHERE key=2
I get the ResultSet
1
|
Thanks. v4.2.031 fixed a bug for DISTINCT operation, which missed the possible data row when there's deleted row with the same value.
BTW, there's no distinct function, so that your sql is
SELECT DISTINCT val AS myRow FROM t1 ;
in fact.
You can use DISTINCT flag on aggregatetion function, or SELECT DISTINCT on (val) val as myrow from t1;... and so on.
|
Thanks, too. I'll try the next version.
|