使用python脚本将数据添加到Postgres DB中并获取字符串索引必须是整数错误

时间:2019-07-01 21:02:58

标签: python psycopg2

我正在尝试使用psycopg2将一些数据添加到我的数据库中。我拥有的数据来自api响应,并且为json.dumps格式。运行脚本时出现错误:string indices must be integers

我尝试了几种解决方案,但对我来说没有任何效果。我的数据库设置为所有字段的文本值。这是我的剧本。

我可以查看任何建议或信息吗? 谢谢

我已经审查了该网站和其他一些网站: https://pynative.com/python-postgresql-insert-update-delete-table-data-to-perform-crud-operations/

img_type = str(dataDB['image_type'])
img_1set = str(dataDB['classes'])

img_score = str(img_1set[0]['score'])
img_class = str(img_1set[0]['class'])

# adding results to db
try:
    connection = psycopg2.connect(user="xxxxxxxxxxxxxxx",
                                  password="xxxxxxxxxxxxxxxxxxxx",
                                  host="xzxzxzxxzxzxzxzxzxzxzxzxzxzxzxzxz",
                                  port="00000",
                                  database="zxc")
    cursor = connection.cursor()
    new_record = (call_status, roundtrip, img_type, img_1set, img_score, img_class)
    insert_command = """ INSERT INTO 
        results_test_one(call_status, roundtrip, img_type, img_1set, 
        img_score, img_class) VALUES (%s,%s,%s,%s,%s,%s)"""
    pprint(insert_command)
    cursor.execute(insert_command, new_record)
    connection.commit()
    count = cursor.rowcount
    print(count, "Record inserted successfully into table")

except (Exception, psycopg2.Error) as error:
    if (connection):
        print("Failed to insert record into table", error)

0 个答案:

没有答案