数据库列中的换行符和空格

发布于 2024-10-27 00:05:32 字数 689 浏览 2 评论 0原文

我无法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟燃烟灭 2024-11-03 00:05:32

你说“”“因为在语句中: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),则尾随的随机空格将干扰索引。 (),您应该能够使用类似这样的单行修复它:

UPDATE table SET type = STRIP(type) WHERE type != STRIP(type);

同时,您可以执行“startswith”类型的查询:

SELECT sensor FROM table WHERE type LIKE 'FEET%'

或使用

SELECT sensor FROM table WHERE STRIP(type) = 'FEET'

进一步评论:

(1) 看起来像你的输出字符串的宽度为 30 个字符(除非它被您的粘贴和我的编辑所破坏:

('FEET\n                         ',)
  .... v....1....v....2....v....3

对额外空格的一种可能解释是列数据类型为 CHAR(30)。如果这是在您的控制之下您可能想将其更改为 VARCHAR(30) 或只是 VARCHAR。请参阅文档的此部分并记下中的“提示”页面的中间。

(2) 似乎没有 SQL 函数能像 str.strip() 一样简单地删除前导/尾随空格。在 PostgreSQL 中,您将需要

TRIM(E' \r\n\t' FROM your_string_expression)

删除前导空格和尾随空格、回车符、换行符和制表符等功能。请参阅文档此处(以及此处;阅读第 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], 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).

天赋异禀 2024-11-03 00:05:32

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.

峩卟喜欢 2024-11-03 00:05:32

尝试使用类似的关键字查询。不是最佳但会起作用。

从表中选择传感器,其中类型为“%FEET%”

Try using a like keyword query. Not as optimal but will work.

SELECT sensor FROM table WHERE type LIKE ‘%FEET%’

好多鱼好多余 2024-11-03 00:05:32

看起来当您插入到表格中时,您没有删除尾线(\n)

>>> a = ["FEET\n",]
>>> print a
['FEET\n']
>>> print a[0]
FEET

>>> 

注意到脚后的额外行。

如果 \n 始终附加到该列中的英尺,您只需从表中选择传感器,其中 type="FEET\n" 即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文