【问题标题】:PostgreSQL- Unable to add record to table [duplicate]PostgreSQL-无法向表中添加记录[重复]
【发布时间】:2021-06-09 17:05:48
【问题描述】:

我对 SQL 很陌生,正在尝试向表中添加一条记录。我调试了我的代码,并且已经成功地创建了一个带有名为“天气”的表的数据库。但是,当我尝试在表中插入记录然后打印表时,终端中没有打印数据。我想知道我做错了什么。谢谢。

conn = psycopg2.connect(database="postgres", user='postgres',password='password', host='127.0.0.1', port= '5432' )
conn.autocommit = True
cursor = conn.cursor()
cursor.execute('''INSERT INTO WEATHER (HUMIDITY, CITY, FEELSLIKE, HIGHLOW, TEMPERATURE) VALUES ('{%s}', '{%s}', '{%s}', '{%s}', '{%s}')'''), (humidityVal, cityVal, feelslikeVal, highlowVal, tempVal)
cursor.execute('''SELECT * from WEATHER''')
result = cursor.fetchall()
print(result)    
conn.close()

【问题讨论】:

  • 我无法重现既没有错误也没有“打印”输出。我得到一个或另一个,这取决于如何充实你不完整的代码。

标签: python sql postgresql


【解决方案1】:

我认为问题出在这一行 你不需要那些括号,也不需要单引号:

...
cursor.execute('INSERT INTO WEATHER (HUMIDITY, CITY, FEELSLIKE, HIGHLOW, TEMPERATURE) VALUES (%s, %s, %s, %s, %s)', (humidityVal, cityVal, feelslikeVal, highlowVal, tempVal))
...

【讨论】:

    猜你喜欢
    • 2020-08-21
    • 1970-01-01
    • 2013-04-02
    • 2021-07-19
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多