![](/img/trans.png)
[英]syntax error at or near “I” while trying to insert rows in postgresql via python script
[英]Insert in PostgreSQL with Python Error “syntax error at or near ”17“ ”
我在使用 Python 將數據插入 PostgreSQL 時遇到問題,但對我顯示了此錯誤。 錯誤:
syntax error at or near "17"
LINE 30: 2021-01-05 17:38:59)
我的 python 代碼如下。 我在 windows 計算機中使用了 psycopg2 和 python 3.7。 非常感謝
try:
dados = GetDatasClimaTempo()
con = psycopg2.connect(host='localhost', database='banco_arduino', user='gabriel', password='password')
cur = con.cursor()
sql = f"""insert into estacao_app_registrometeorologico
(temperaturaAmbiente,
radiacaoDifusa,
radiacaoGlobal,
pressao,
altitudeReal,
indiceUltravioleta,
precipitacaoInstantanea,
precipitacaoAcumulada,
umidade,
velocidadeVento,
direcaoVento,
anguloVento,
radiometroTermico,
timestamp)
values
({dados['temperatura']},
{truncate(random.uniform(10.5, 100.5),2)},
{truncate(random.uniform(10, 3000),2)},
{dados['pressao']},
{truncate(random.uniform(10.5, 100.5),2)},
{truncate(random.uniform(1, 11),2)},
{truncate(random.uniform(10.5, 100.5),2)},
{truncate(random.uniform(1, 15),2)},
{dados['umidade']},
{dados['velocidadeVento']},
{dados['direcaoVento']},
{truncate(random.uniform(0, 360),2)},
{truncate(random.uniform(10.5, 100.5),2)},
{datetime.datetime.strptime(dados['timestamp'], '%Y-%m-%d %H:%M:%S')})"""
cur.execute(sql)
con.commit()
cur.execute('select * from estacao_app_registrometeorologico')
recset = cur.fetchall()
for rec in recset:
print (rec)
con.close()
except Exception as ex:
print(ex)
簡短的回答是將strptime()
的結果用單引號括起來:
sql = f"""insert into estacao_app_registrometeorologico
...
'{datetime.datetime.strptime(dados['timestamp'], '%Y-%m-%d %H:%M:%S')}')"""
更長的答案是根據此處找到的警告重寫您的代碼以使用參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.