1

I have simple query which contains among others:

SELECT ST_Within(ST_GeomFromText('POINT(lon lat)', 4326), buildings.geom) AS is_within

So I expect the result column is_within to be Boolean value. And this is what happening when I test this on QGIS. Then I integrate it within some java code using the well known standard connection with driver class (nothing unusual):

Class.forName(DB_CLASS);
Connection connection = DriverManager.getConnection(DB_ADDRESS, DB_USER_NAME, DB_PASSWORD);
if (connection != null) {
    Statement statement = connection.createStatement();
    String SQL = ...;
    ResultSet resultSet = statement.executeQuery(SQL);
    while (resultSet.next()) {
        System.out.println(resultSet.getString(1));  //result is letters and not booleans
    }
    resultSet.close();
    statement.close();
    connection.close(); 
}

Now from some reason the result is "t" instead of "true" and "f" instead of "false". Why is this happening?

2 Answers 2

4

If you change the line

System.out.println(resultSet.getString(1));

to

System.out.println(resultSet.getBoolean(1));

you will get back a boolean value instead of a string.

3

The t/f vs. true/false is just a display issue. It is a boolean internally in PostgreSQL but depending on what driver you are using a boolean value takes on different representation with t/f being the standard canonical form for psql, pgAdmin and QGIS. In MSAccess it becomes -1.

In SQL ST_Within(..) = true vs. ST_Within = t vs. ST_Within all mean the same thing.

If you do this for example in pgAdmin,

SELECT true;

you get t

But you can cast a boolean to text and then it shows true/false

SELECT ST_Within('LINESTRING(1 2, 3 4, 2 4)'::geometry, 
    'POINT(3 4)'::geometry)::text;

Outputs:

false

Why SELECT t; is not considered boolean is a mystery to me :) Confused now :)

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.