Error: Column does not exist

我怕爱的太早我们不能终老 提交于 2019-11-26 12:47:53

问题


I have been able to link PostgreSQL to java. I have been able to display all the records in the table, however I unable to perform delete operation.

Here is my code:

con = DriverManager.getConnection(url, user, password);
String stm = \"DELETE FROM hostdetails WHERE MAC = \'kzhdf\'\";
pst = con.prepareStatement(stm);
pst.executeUpdate(); 

Please note that MAC is a string field and is written in capital letters. This field does exist in the table.

The error that I am getting:

SEVERE: ERROR: column \"mac\" does not exist


回答1:


When it comes to Postgresql and entity names (Tables, Columns, etc.) with UPPER CASE letters, you need to "escape" the word by placing it in "". Please refer to the documentation on this particular subject. So, your example would be written like this:

String stm = "DELETE FROM hostdetails WHERE \"MAC\" = 'kzhdf'";

On a side note, considering you are using prepared statements, you should not be setting the value directly in your SQL statement.

con = DriverManager.getConnection(url, user, password);
String stm = "DELETE FROM hostdetails WHERE \"MAC\" = ?";
pst = con.prepareStatement(stm);
pst.setString(1, "kzhdf");
pst.executeUpdate();


来源:https://stackoverflow.com/questions/15275971/error-column-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!