我犯了一个奇怪的错误,但我不明白为什么会这样。基本上,我使用SELECT查询从数据库中获取一些数据,它会生成一个错误。
错误与“WHERE traseu=”+traseu+“;”有关PostgreSQL数据库中的traseu列类型为“text”;

protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);

            String traseu = values[0];
            String sql = "SELECT id, ST_x(geom) AS x, ST_y(geom) AS y, tip AS tr, traseu, denumire, adresa, poza FROM modul WHERE traseu = " + traseu + ";";

它给了我以下错误:
01-08 15:30:05.435  23635-23635/com.example.nsomething E/com.example.nsomething.main.Harta﹕ query failed to execute: org.postgresql.util.PSQLException: ERROR: operator does not exist: text = integer
    Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
    Position: 112

任何暗示。

最佳答案

不要将用户提供的值连接到SQL文本中。
这是危险和错误的:

String sql = "SELECT ... WHERE traseu = " + traseu + ";";
                                        ^^^^^^^^^^^^^^^
                                        Bad, don't do this

对于一个整数来说,这并不是很危险,但它仍然是一个可怕的习惯。别这么做。
相反,你应该use parameterised statements
String sql = "SELECT ... WHERE traseu = ?;";

PreparedStatement st = conn.createPrepared(sql);
st.setInt(1, traseu);

见:
http://bobby-tables.com
http://en.wikipedia.org/wiki/SQL_injection

10-04 19:23