Search in PostgreSQL table

女生的网名这么多〃 提交于 2019-12-12 01:37:35

问题


I have this table in which I would like to implement search filter.

CREATE TABLE ACCOUNT(
 ID INTEGER NOT NULL,
 USER_NAME TEXT,
 PASSWD TEXT,
 FIRST_NAME TEXT,
 LAST_NAME TEXT,
 LAST_LOGIN DATE,
 DATE_REGISTERED DATE,
 ROLE INTEGER,
 CAN_LOGIN INTEGER
)
;

-- ADD KEYS FOR TABLE ACCOUNT

ALTER TABLE ACCOUNT ADD CONSTRAINT KEY1 PRIMARY KEY (ID)
;

String searchString = "32";

SELECT * FROM ACCOUNT
WHERE " + searchString + " IN (ID, USER_NAME, FIRST_NAME, LAST_NAME) ORDER BY %S %S offset ? limit ?;

I get error

serverError: class java.lang.IndexOutOfBoundsException Index: 0, Size: 0

Also I don't want to specify every table column in which I want to search. Is there a way to implement search in all columns by default? And just specify one column in which I don't want to search?


回答1:


You have a type conversion problem with the IN list. These all have to be the same type, so they are converted to the type of what is being compared. And, there is a failure to convert strings to ints.

If you include single quotes, then your query should work:

WHERE '" + searchString + "' IN (ID, USER_NAME, FIRST_NAME, LAST_NAME) 


来源:https://stackoverflow.com/questions/36519762/search-in-postgresql-table

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