0

Was following a tutorial on medium about postgres. Here's the article - link

At some point he tries to create a table and insert test values. Later he tries to print the table using some code which I have pasted below -

import psycopg2

hostname = 'localhost'
username = 'postgres' # the username when you create the database
password = '***' #change to your password
database = 'quotes'

def queryQuotes( conn ) :
    cur = conn.cursor()
    cur.execute( """select * from quotes_content""" )
    rows = cur.fetchall()

    for row in rows :
        print (row[1])

conn = psycopg2.connect( host=hostname, user=username, password=password, dbname=database )
queryQuotes( conn )
conn.close()

I followed the same and created a table named jobs(mind the case).

create table jobs(id serial primary key, job-link text, company text, job-title text)
insert into jobs(job-link, company, job-title) values('test-link','test-company','test-job')

When I created my queryJobs function in the execute() line, I tried the following select statements but all of them return a SyntaxError -

"""select * from jobs""" returns psycopg2.errors.UndefinedTable: relation "jobs" does not exist
"""select * from "jobs"""" returns SyntaxError: EOL while scanning string literal
"select * from "jobs"" returns SyntaxError: invalid syntax
"""select * from 'jobs'""". returns psycopg2.errors.SyntaxError: syntax error at or near "'jobs'"

I tried using multiple versions of quotes because I read in this answer that there is some case sensitivity issue in Postgres tables. Refer the answer given by @Mitzi in this question.

Arif
  • 309
  • 3
  • 10
  • 1
    The first one is correct, but it indicates the table doesn't exist. Did you commit the table create? is it the same database? same schema? – Keith Aug 16 '20 at 07:28
  • @Keith I just used the two statements I stated in my question body, the `create` and `insert` statement. No `commit` and I am not sure about the schema. I am just following the tutorial, so not sure about those terms. – Arif Aug 16 '20 at 07:49
  • Please edit your question to include your table creation DDL for table, `jobs`. The column names, `job-link` and `job-title` are invalid identifiers because of the hyphens. – Mike Organek Aug 16 '20 at 08:28
  • The case sensitivity issue only arises if you create a quoted table name("SomeTable") that uses anything other then lower case letters. This means if your table create script is correct(leaving aside hyphen issue) you do not have to quote the table name. If you do want to deal with quoted names use quote_ident('SomeTable'). – Adrian Klaver Aug 16 '20 at 15:39

0 Answers0