简体   繁体   中英

why the select query through python throwing syntax error?

I am trying to connect to a Postgres DB and execute a simple select query. But I am getting errors. Below is the sample program

import psycopg2 as ps

param_dic = ps.connect(
    host="localhost",
    database="Test_DB",
    user="username",
    password="password"
)

cur = param_dic.cursor()
cur.execute("select * from  <schema_name>.'employee_tbl'") # I am 
rows = cur.fetchall()

for r in rows:
    print(r)

I get below error:

psycopg2.errors.SyntaxError: syntax error at or near "'tbl_name'"
LINE 1: select * from  <schema_name>.'tbl_name'

If I use

cur.execute("select * from <schema_name>.'Employee_TBL'")

Still, I get the same error.

Note: when I check my pgAdmin, I see below as the table name

<schema_name>.Employee_TBL

So, what is the right way to execute SQL queries on a Postgres table?

将 <schema_name>.'Employee_TBL'" 替换为真值:dbo.Employee_TBL 或数据库中的其他模式名称

尝试删除 'emloyee_tbl' 周围的引号

Maybe it's just a typo, use double quotes for the table if you need to.
Single quotes indicate a string literal.

import psycopg2
conn = psycopg2.connect(...)
cur = conn.cursor()
    
cur.execute('SELECT * FROM public.servers')
print(len(cur.fetchall()))

cur.execute('SELECT * FROM PUBLIC."servers"')
print(len(cur.fetchall()))

Out:

6
6

The overriding issue is that you created a table name with quoted mixed case. This means that from then on you need to quote the name to be able to use it:

create table "Mixed_Case" (id int, fld_1 varchar);
select * from mixed_case;
ERROR:  relation "mixed_case" does not exist
LINE 1: select * from mixed_case;

select * from "Mixed_Case";
 id | fld_1 
----+-------
(0 rows)

--NOTE the double quotes.

For more information see 4.1.1. Identifiers and Key Words

For the psycopg2 case:

import psycopg2
con = psycopg2.connect("dbname='test' host='localhost' user='aklaver'")
cur = con.cursor()
# Single quoted
cur.execute("select * from public.'Mixed_Case'")
SyntaxError: syntax error at or near "'Mixed_Case'"
LINE 1: select * from public.'Mixed_Case'
# Identifiers need double quotes

# Changing to double quotes
cur.execute('select * from public."Mixed_Case"')
# No error.
# You can also use Postgres functions
cur.execute("select * from quote_ident('public.Mixed_Case')")

For more on Postgres functions and methods see string functions

Also in psycopg2 there is the sql module that allows you to work with identifiers:

from psycopg2 import sql
sql_str = sql.SQL("select * from public.{}").format(sql.Identifier('Mixed_Case'))
print(sql_str.as_string(con))                                                                                                                                             
select * from public."Mixed_Case"
cur.execute(sql_str) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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