在 postgreSQL 中选择大写的表名无法正常工作

3

我正在使用Windows7和Python3.4.4上的psycopg2。

我想从大写名称的表中获取数据,但我无法弄清楚。有人能帮我吗?

总是返回这样的结果 relation "table" does not exist 我想让"table"变成大写。

这是我的代码 import psycopg2

class KindOfCoupons:

   def get_coupons(self, cur, names):
       coupons = {}
       for name in names:
           coupons[name] = cur.execute("SELECT * FROM \"" + name + "\" ;")
       return coupons

   def connect_redshift(self):
       conn = psycopg2.connect("dbname=dbname host=host user=user password=password port=000")
       return conn.cursor()

   def get_coupon_used_type(self):
       cur = self.connect_redshift()
       names = ["TABLE", "TABLE_B", "TABLE_C"]
       coupons = self.get_coupons(cur, names)
       coupons[names[0]][0]

1
据我所知,表名和列名是不区分大小写的。如果有错误,您遇到了什么错误? - DeepSpace
1个回答

4

谢谢回答!但是对我来说,表已经创建好了,我想要访问它。如果表已经创建好了,我无法获取到它吗? - Masaru Iwasa
只是不要使用引号:"SELECT * FROM " + name + " ;"。无论如何,你应该更倾向于为每个表格编写一个选择函数,因为连接查询是一种不好的实践,并且无法对FROM子句进行参数化。 - DeepSpace
1
是的,我之前尝试过这种方式,像 cur.execute("SELECT * FROM " + name + " ;")。 但总是返回 relation 'table' does not exist。我需要将它改为大写... - Masaru Iwasa
这对我有用:cur.execute('SELECT * FROM table WHERE "columnName" = \'value\'')(适用于字符串类型的列)。 - Raphael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接