编写python postgres insert命令的最佳方法

编写python postgres insert命令的最佳方法,python,json,postgresql,Python,Json,Postgresql,我是一个有点新手,我被这个postgres插入步骤卡住了 我的挑战是从存储在列表中的json中提取Dict,并尝试从Dict中提取值并将其保存到postgres DB 任何关于如何正确书写的帮助都将不胜感激 这是分页符行下的DB连接字符串,是用于DB插入的代码 import psycopg2 '''DATABASE CONNECTION SETTINGS''' def dbconnect(): """Function returns settings for db connecti

我是一个有点新手,我被这个postgres插入步骤卡住了

我的挑战是从存储在列表中的json中提取Dict,并尝试从Dict中提取值并将其保存到postgres DB

任何关于如何正确书写的帮助都将不胜感激

这是分页符行下的DB连接字符串,是用于DB插入的代码

import psycopg2

'''DATABASE CONNECTION SETTINGS'''


def dbconnect():
    """Function returns settings for db connection."""
    dbauth = psycopg2.connect("dbname='databse' user='username' \
        host='dbhost' password='password'")

    return dbauth

天气更新(dbconnect(),getweather())

当我运行代码时,它会抛出以下错误:

Traceback (most recent call last):
  File "weatherDb.py", line 57, in <module>
    weatherupdate(dbconnect(), getweather())
  File "weatherDb.py", line 53, in weatherupdate
    cursor.execute(query)
psycopg2.ProgrammingError: column "l01" does not exist
LINE 1: ...d_type_id, swell_dir_worded, sort_order ) VALUES (l01, l02, ...
回溯(最近一次呼叫最后一次):
文件“weatherDb.py”,第57行,在
天气更新(dbconnect(),getweather())
weatherupdate中第53行的文件“weatherDb.py”
cursor.execute(查询)
psycopg2.ProgrammingError:列“l01”不存在
第1行:…d类型id、膨胀方向、排序顺序)值(l01、l02。。。
我确信这是不正确的,所以任何帮助和指导都是很好的


提前谢谢。

请添加用于数据库连接的导入文件。我已经用此信息更新了最初的帖子,除了在一些事情上“绕了很长一段路”(不需要分配所有的L*变量)之外,它看起来很好。它真的工作了吗?如果没有,什么不工作?它抛出了这个错误:回溯(最后一次调用):文件“weatherDb.py”,第57行,在weatherupdate(dbconnect(),getweather())文件“weatherDb.py”,第53行,在weatherupdate游标中。执行(查询)psycopg2.ProgrammingError:列“l01”不存在第1行:…d_type_id,sweel_dir_worded,sort_order)值(l01,l02,…我正在寻找一种更简单的方法来编写此代码。当我没有将“l”放在变量前面时,它抛出了一个错误。您不需要在任何东西前面加l…这些是原始字符串…您从未使用过实际的变量。
'(l01、l02、l03…”
。请尝试创建一个字符串。您是否假设所有列都是字符串?
query = """INSERT INTO weather (state_time_zone, time_zone, product_name, [SNIP]) 
            VALUES (%s, %s, %s, [SNIP] ) """
cursor.execute(query, (l01, l02, l03 [SNIP])
query = """INSERT INTO weather (state_time_zone, time_zone, product_name, [SNIP]) 
            VALUES (%s, %s, %s, [SNIP] ) """
cursor.execute(query, (l01, l02, l03 [SNIP])