I cannot find the records using the WHERE CLAUSE :
SELECT sensor FROM table WHERE type = 'FEET'
There are records in the table whose 'type' column values are 'FEET', so I suppose it's something wrong about the format. the result would be something more than 'FEET' so that开发者_高级运维 it simply can not find those equal to 'FEET'.
To find out what the records in column type looks like, I used the following codes to print them out:
queryTem='SELECT type FROM tide_data'
curs.execute(queryTem)
resultsTem=curs.fetchall()
for resultTem in resultsTem:
print resultTem
print resultTem[0]
the resultTem turns out something like:
('FEET\n ',)
while resultTem[0] turns out like: FEET, which should be 'FEET\n'.Based on the information above, is there any way I can rewrite the WHERE CLAUSE statement and get the correct records? Since in the statement: WHERE a = value.. a should be a column name, I can not think of a way to do it.
Someone can help me out here? thanks..
You say """Since in the statement: WHERE a = value.. a should be a column name""". Not so, a
can be any expression.
You say """the resultTem turns out something like: ('FEET\n ',) while resultTem[0] turns out like: FEET, which should be 'FEET\n'"". Instead of print resultTem[0]
, use print repr(resultTem[0])
. Get into the habit of using repr()
in debug situations.
As others have pointed out, your data needs scrubbing. If there is an index on that type
column, the trailing random whitespace will interfere with the indexing. If your database has a function (named e.g. STRIP) that works like Python's str.strip()
, you should be able to fix it with a one-liner something like this:
UPDATE table SET type = STRIP(type) WHERE type != STRIP(type);
In the meantime, you can do a "startswith" type of query:
SELECT sensor FROM table WHERE type LIKE 'FEET%'
or use
SELECT sensor FROM table WHERE STRIP(type) = 'FEET'
Further comments:
(1) It looks like your output string is 30 characters wide (unless it got mangled by your pasting and my editing:
('FEET\n ',)
.... v....1....v....2....v....3
One possible explanation for the extra spaces is that the column data type is CHAR(30)
. If this is under your control you might like to change it to VARCHAR(30)
or just VARCHAR
. See this section of the docs and note the "Tip" in the middle of the page.
(2) It seems there's no SQL function as easy as str.strip()
for removing leading/trailing whitespace. In PostgreSQL you are going to need something like
TRIM(E' \r\n\t' FROM your_string_expression)
to remove leading and trailing spaces, carriage returns, line feeds, and tabs. See docs here (and here; read section 4.1.2.2. String Constants with C-Style Escapes).
resultTem[0] is not "FEET", it's "FEET\n". You'll need to scrub your data if you want your query to work.
Try using a like keyword query. Not as optimal but will work.
SELECT sensor FROM table WHERE type LIKE ‘%FEET%’
Looks like when you are inserting to your table, you didn't strip the endline (\n)
>>> a = ["FEET\n",]
>>> print a
['FEET\n']
>>> print a[0]
FEET
>>>
notice the extra line after feet.
you can just select sensor from table where type="FEET\n"
if \n is always appended to feet in that column.
精彩评论