数据库列中的换行符和空格
我无法使用 WHERE CLAUSE 找到记录:
SELECT sensor FROM table WHERE type = 'FEET'
表中存在“type”列值为“FEET”的记录,因此我认为格式有问题。结果将不仅仅是“FEET”,因此它根本无法找到等于“FEET”的内容。
为了找出列类型中的记录是什么样子,我使用以下代码将它们打印出来:
queryTem='SELECT type FROM tide_data'
curs.execute(queryTem)
resultsTem=curs.fetchall()
for resultTem in resultsTem:
print resultTem
print resultTem[0]
resultTem 结果类似于:
('FEET\n ',)
而 resultTem[0] 结果类似于: FEET,应该是 'FEET\n'.Based根据上面的信息,有什么方法可以重写 WHERE CLAUSE 语句并获得正确的记录吗?由于在语句中: WHERE a = value.. a 应该是列名,我想不出办法来做到这一点。
有人可以帮我吗?谢谢..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你说“”“因为在语句中:WHERE a = value.. a应该是列名”“”。事实并非如此,
a
可以是任何表达式。你说“”“resultTem 结果类似于: ('FEET\n ',) 而 resultTem[0] 结果类似于: FEET,应该是 'FEET\n'"”。而不是
print resultTem[ 养成在调试情况下使用
repr()
的习惯。0],使用
print repr(resultTem[0])
。正如其他人指出的那样, 您的数据需求如果该type
列上有索引,如果您的数据库有一个类似于 Python 的str.strip 的函数(例如名为 STRIP),则尾随的随机空格将干扰索引。 ()
,您应该能够使用类似这样的单行修复它:同时,您可以执行“startswith”类型的查询:
或使用
进一步评论:
(1) 看起来像你的输出字符串的宽度为 30 个字符(除非它被您的粘贴和我的编辑所破坏:
对额外空格的一种可能解释是列数据类型为
CHAR(30)
。如果这是在您的控制之下您可能想将其更改为VARCHAR(30)
或只是VARCHAR
。请参阅文档的此部分并记下中的“提示”页面的中间。(2) 似乎没有 SQL 函数能像
str.strip()
一样简单地删除前导/尾随空格。在 PostgreSQL 中,您将需要删除前导空格和尾随空格、回车符、换行符和制表符等功能。请参阅文档此处(以及此处;阅读第 4.1.2.2 节。带有 C 风格转义的字符串常量)。
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]
, useprint repr(resultTem[0])
. Get into the habit of usingrepr()
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'sstr.strip()
, you should be able to fix it with a one-liner something like this:In the meantime, you can do a "startswith" type of query:
or use
Further comments:
(1) It looks like your output string is 30 characters wide (unless it got mangled by your pasting and my editing:
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 toVARCHAR(30)
or justVARCHAR
. 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 liketo 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] 不是“FEET”,而是“FEET\n”。如果您希望查询正常工作,则需要清理数据。
resultTem[0] is not "FEET", it's "FEET\n". You'll need to scrub your data if you want your query to work.
尝试使用类似的关键字查询。不是最佳但会起作用。
从表中选择传感器,其中类型为“%FEET%”
Try using a like keyword query. Not as optimal but will work.
SELECT sensor FROM table WHERE type LIKE ‘%FEET%’
看起来当您插入到表格中时,您没有删除尾线(\n)
注意到脚后的额外行。
如果 \n 始终附加到该列中的英尺,您只需从表中选择传感器,其中 type="FEET\n" 即可。
Looks like when you are inserting to your table, you didn't strip the endline (\n)
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.