3

I'm using psycopg2 on windows7 and python3.4.4.

I'd like to get data from tables of uppercase name, but I couldn't figure it out. Can anyone help me?

Always retuturn like this relation "table" does not exist I want to make "table" uppercase.

here's my code 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]
Masaru Iwasa
  • 521
  • 1
  • 6
  • 9
  • 1
    As far as I know table and column names are case insensitive. What is the error you are getting, if any? EDIT - see this question: http://stackoverflow.com/questions/21796446/postgres-case-sensitivity – DeepSpace May 09 '16 at 11:59

1 Answers1

4

PostgresSQL column and table names are case insensitive, unless you surround them with quotes (like you do, "SELECT * FROM \"" + name + "\" ;").

See this answer: https://stackoverflow.com/a/21798517/1453822

Community
  • 1
  • 1
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • Thanks answered! But for me, the table is already created, and I want access it. I can't get the table if it's already created? – Masaru Iwasa May 09 '16 at 12:17
  • Simply don't use the quotes: ``"SELECT * FROM " + name + " ;"``. Anyway, you should prefer to have a function for selecting from each table, as concatenating queries is a bad practice, and it's impossible to parameterize the FROM clause. – DeepSpace May 09 '16 at 12:20
  • 1
    yes, I have tried that way before like `cur.execute("SELECT * FROM " + name + " ;")`. But alway return `relation 'table' does not exist` I need make it uppercase... – Masaru Iwasa May 09 '16 at 12:27
  • This worked for me: `cur.execute('SELECT * FROM table WHERE "columnName" = \'value\'')` (for a column that is of type string). – Raphael Jul 15 '22 at 14:43