如何调用Postgresql数据库中有特殊字符的字段名?

2024-05-20 02:32:10 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我如何处理postgres数据库中具有“/”等字符的字段名吗。我的数据库有一个名为CD/CSD的字段(列头),当我试图在该字段上运行查询时,我只会得到错误消息。另外,如果我在名称周围加上单引号,我不会收到错误消息,但是在运行查询时不会返回任何内容。如有任何帮助,将不胜感激。下面是我的代码的样子:

import pandas as pd
import numpy as np
import psycopg2
from sqlalchemy import create_engine # database connection
engine = create_engine('postgresql://postgres:xxxxx@localhost:xxx/My_Database')
province_selected = raw_input("Type name of province desired: ")
df = pd.read_sql("SELECT * FROM my_table Where CD/CSD=%s", engine, params=(province_selected,))

Tags: import数据库消息as错误createcdpostgres
1条回答
网友
1楼 · 发布于 2024-05-20 02:32:10

Also, if I put single quotes around the name, I don't get an error message but nothing is returned when I run my query.

SQL中的单引号表示字符串。当你说

Where 'CD/CSD'=%s

你的意思是%s实际上是6个字符的字符串CD/CSD。在

我不确定Postgres,但我敢打赌它遵守SQL标准,引用标识符,这就是列名。也就是说(有点奇怪,但这是SQL),标识符用双引号引起来。是的,这就是为什么Python有三个引号:

^{pr2}$

相关问题 更多 >