复杂 SQL 查询在 Postgresql pgAdmin 4 中有效,但在 Python 中无效
Complex SQL query works in Postgresql pgAdmin 4 but not Python
这个查询:
select product.width, product.height
from product
inner join product_template on product.prodtempindex = product_template.prodtempindex
inner join painting on painting.pntindex = product.pntindex
where painting.catalognumber = 'bg0025' and product.prodtempindex = 2
适用于 Postgresql pgAdmin4。
此查询的简化版本适用于 pgAdmin 和 Python:
cur.execute("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s",[number])
'number'是上面bg0025的变量替换。
问题是,我无法解决如何在 Python 查询中包含“and product.prodtempindex = 2”子句。我收到语法错误或参数过多。
import psycopg2
import csv
csv_file = "C:/BG/business/images/master_sizes.csv"
conn = psycopg2.connect(dbname="bgartwork", user="postgres", host="billgiacalone.com", password="Jg116162!")
cur = conn.cursor()
with open(csv_file,'r') as csvfile:
imagesizes = csv.reader(csvfile)
for row in imagesizes:
number = row[0][0:6:1]
dimension = row[1]
inches = row[2]
cur.execute("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s",[number]) "and product.prodtempindex = 2"
dbrow = cur.fetchall()
for drow in dbrow:
print(number,drow)
ERROR:
File "C:\Users\xxx\product_gen\check_prod_size.py", line 19
cur.execute("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s",[number]) "and product.prodtempindex = 2"
^
SyntaxError: invalid syntax
根据 Parameters:
import psycopg2
con = psycopg2.connect(dbname="test", host='localhost', user='postgres')
cur = con.cursor()
number = 1
#mogrify returns an adapted query string. Used here to show that the
#query is correctly built. Substitute execute for actual usage.
cur.mogrify("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s and product.prodtempindex = %s",[number, 2])
'select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = 1 and product.prodtempindex = 2'
这个查询:
select product.width, product.height
from product
inner join product_template on product.prodtempindex = product_template.prodtempindex
inner join painting on painting.pntindex = product.pntindex
where painting.catalognumber = 'bg0025' and product.prodtempindex = 2
适用于 Postgresql pgAdmin4。
此查询的简化版本适用于 pgAdmin 和 Python:
cur.execute("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s",[number])
'number'是上面bg0025的变量替换。
问题是,我无法解决如何在 Python 查询中包含“and product.prodtempindex = 2”子句。我收到语法错误或参数过多。
import psycopg2
import csv
csv_file = "C:/BG/business/images/master_sizes.csv"
conn = psycopg2.connect(dbname="bgartwork", user="postgres", host="billgiacalone.com", password="Jg116162!")
cur = conn.cursor()
with open(csv_file,'r') as csvfile:
imagesizes = csv.reader(csvfile)
for row in imagesizes:
number = row[0][0:6:1]
dimension = row[1]
inches = row[2]
cur.execute("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s",[number]) "and product.prodtempindex = 2"
dbrow = cur.fetchall()
for drow in dbrow:
print(number,drow)
ERROR:
File "C:\Users\xxx\product_gen\check_prod_size.py", line 19
cur.execute("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s",[number]) "and product.prodtempindex = 2"
^
SyntaxError: invalid syntax
根据 Parameters:
import psycopg2
con = psycopg2.connect(dbname="test", host='localhost', user='postgres')
cur = con.cursor()
number = 1
#mogrify returns an adapted query string. Used here to show that the
#query is correctly built. Substitute execute for actual usage.
cur.mogrify("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s and product.prodtempindex = %s",[number, 2])
'select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = 1 and product.prodtempindex = 2'