除非 WHERE 子句硬编码,否则 Psycopg2 不是空结果
Psycopg2 not empty result unless WHERE clause hard coded
我正在尝试使用 psycopg2 进行非常基本的查询,但除非我对参数进行硬编码,否则它不会给我结果,我做错了什么?可能是我在这里有午夜大脑,但我不这么认为...
这不起作用:
query = """SELECT TRIM(TRAILING FROM symbol) as symbol, timestamp, open, close, high, low, volume
FROM trades_1_min
WHERE symbol=%(symbol)s
ORDER BY id DESC LIMIT 1"""
with db_conn.cursor() as cursor:
cursor.execute(query, {'symbol': symbol})
res = cursor.fetchall()
cursor.close()
return res
return None
而如果我在输入 [... symbol='%(symbol)s' ...] 周围加上引号,我可以看到它实际上将查询参数放在那里,因为我收到以下错误:
Traceback (most recent call last):
File "testlol.py", line 22, in <module>
main()
File "testlol.py", line 20, in main
print(get_latest("amd"))
File "testlol.py", line 12, in get_latest
cursor.execute(query, {'symbol': symbol})
psycopg2.errors.SyntaxError: syntax error at or near "amd"
LINE 3: WHERE symbol=''amd''
现在,如果我只是 运行 psql 中的查询,我就会得到答案。是的,我连接到正确的数据库,正确的用户等等。一个更简单的查询可以确认这一点。
import psycopg2
db_conn = psycopg2.connect(dbname="pi", user="pi", password="raspberry")
def lol():
cursor = db_conn.cursor()
cursor.execute('SELECT * FROM trades_1_min LIMIT 1')
res = cursor.fetchall()
print(res)
def get_latest(symbol):
query = """SELECT TRIM(TRAILING FROM symbol) as symbol, timestamp, open, close, high, low, volume
FROM trades_1_min
WHERE symbol=%(symbol)s
ORDER BY id DESC LIMIT 1"""
with db_conn.cursor() as cursor:
cursor.execute(query, {'symbol': symbol})
res = cursor.fetchall()
cursor.close()
return res
return None
def main():
lol()
#print(get_latest("amd"))
main()
这里是 table 结构:
Table structure
所以我遇到的问题真的很愚蠢。我的符号以大写字母存储。所以我要求 'amd' 但我只有 'AMD',所以很合逻辑,它没有找到任何东西......所以这确实是一个午夜大脑的情况。
我正在尝试使用 psycopg2 进行非常基本的查询,但除非我对参数进行硬编码,否则它不会给我结果,我做错了什么?可能是我在这里有午夜大脑,但我不这么认为...
这不起作用:
query = """SELECT TRIM(TRAILING FROM symbol) as symbol, timestamp, open, close, high, low, volume
FROM trades_1_min
WHERE symbol=%(symbol)s
ORDER BY id DESC LIMIT 1"""
with db_conn.cursor() as cursor:
cursor.execute(query, {'symbol': symbol})
res = cursor.fetchall()
cursor.close()
return res
return None
而如果我在输入 [... symbol='%(symbol)s' ...] 周围加上引号,我可以看到它实际上将查询参数放在那里,因为我收到以下错误:
Traceback (most recent call last):
File "testlol.py", line 22, in <module>
main()
File "testlol.py", line 20, in main
print(get_latest("amd"))
File "testlol.py", line 12, in get_latest
cursor.execute(query, {'symbol': symbol})
psycopg2.errors.SyntaxError: syntax error at or near "amd"
LINE 3: WHERE symbol=''amd''
现在,如果我只是 运行 psql 中的查询,我就会得到答案。是的,我连接到正确的数据库,正确的用户等等。一个更简单的查询可以确认这一点。
import psycopg2
db_conn = psycopg2.connect(dbname="pi", user="pi", password="raspberry")
def lol():
cursor = db_conn.cursor()
cursor.execute('SELECT * FROM trades_1_min LIMIT 1')
res = cursor.fetchall()
print(res)
def get_latest(symbol):
query = """SELECT TRIM(TRAILING FROM symbol) as symbol, timestamp, open, close, high, low, volume
FROM trades_1_min
WHERE symbol=%(symbol)s
ORDER BY id DESC LIMIT 1"""
with db_conn.cursor() as cursor:
cursor.execute(query, {'symbol': symbol})
res = cursor.fetchall()
cursor.close()
return res
return None
def main():
lol()
#print(get_latest("amd"))
main()
这里是 table 结构: Table structure
所以我遇到的问题真的很愚蠢。我的符号以大写字母存储。所以我要求 'amd' 但我只有 'AMD',所以很合逻辑,它没有找到任何东西......所以这确实是一个午夜大脑的情况。