简体   繁体   English

python:postgresql:区分大小写的列名

[英]python: postgresql: casesensitive column name

I'm trying to do a select query on a psql DB. 我正在尝试在psql数据库上执行选择查询。 But my columnname is "Tagnaam", so case sensitive. 但是我的列名是“ Tagnaam”,所以区分大小写。

I'm trying this query: 我正在尝试以下查询:

cur.execute("SELECT Tagnaam FROM opc_taginstellingen")

I've read the solution would be to use double quotes on the column name, but that doesn't fix it. 我读过解决方案是在列名上使用双引号,但这不能解决问题。

What I also tried: 我还尝试了什么:

cur.execute("SELECT "Tagnaam" FROM opc_taginstellingen")
cur.execute("SELECT ""Tagnaam"" FROM opc_taginstellingen")
cur.execute("SELECT '"Tagnaam"' FROM opc_taginstellingen")

None of the above worked. 以上都不起作用。

The error it gives: 它给出的错误:

    cur.execute("SELECT Tagnaam FROM opc_taginstellingen")
psycopg2.ProgrammingError: column "tagnaam" does not exist
LINE 1: SELECT Tagnaam FROM opc_taginstellingen
               ^

How can I solve this problem? 我怎么解决这个问题?

Thanks in advance. 提前致谢。

在查询周围使用"Tagnaam"和单引号( ' ' )应该可以:

cur.execute('SELECT "Tagnaam" FROM opc_taginstellingen')

Postgresql and all databases (Oracle, Db2...) usually treat column names in a case-insensitive way, so it should no matter how do you write the field name in the query. Postgresql和所有数据库(Oracle,Db2 ...)通常以不区分大小写的方式对待列名,因此无论您如何在查询中编写字段名,都应如此。 Try first all in the psql client. 首先尝试在psql客户端中进行所有操作。 If it works, you can add it to the python code, but be careful, if you use "" inside the string, you should write the string like 'string with "quotedString" inside". The case where the single quotes and double quotes swap can also happen. 如果可行,则可以将其添加到python代码中,但是要小心,如果在字符串中使用“”,则应将字符串写为“在字符串中带有“ quotedString”的字符串”。单引号和双引号交换也可能发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM