Python模块psycopg2找不到引用
我安装了psycopg2模块,因为我想将blob文件发送到数据库。但我有个错误Python模块psycopg2找不到引用,python,python-3.x,psycopg2,Python,Python 3.x,Psycopg2,我安装了psycopg2模块,因为我想将blob文件发送到数据库。但我有个错误 AttributeError: 'psycopg2.extensions.connection' object has no attribute 'execute' 这是我的代码,有什么问题吗 conn = psycopg2.connect(database='SECRET', user='SECRET', password='SECRET', host='S
AttributeError: 'psycopg2.extensions.connection' object has no attribute 'execute'
这是我的代码,有什么问题吗
conn = psycopg2.connect(database='SECRET', user='SECRET',
password='SECRET', host='SECRET',
port=5432)
cursor = conn.cursor()
cursor.execute("SELECT gen_random_uuid();")
id = cursor.fetchone()[0]
drawing = open("cat.jpg", 'rb').read()
conn.execute("INSERT INTO files(uuid, filename, data) VALUES(%s,%s,%s)", (id, "cat.jpg",
psycopg2.Binary(drawing)))
conn.commit()
第二次执行使用连接而不是游标。 替换此行:
conn.execute("INSERT INTO files(uuid, filename, data) VALUES(%s,%s,%s)", (id, "cat.jpg", psycopg2.Binary(drawing)))
与:
应该解决这个问题。您应该在安装之后安装程序包
pip install psycopg2 binary
,我引用
您还可以获得一个独立的包,不需要编译器或外部库
cursor.execute("INSERT INTO files(uuid, filename, data) VALUES(%s,%s,%s)", (id, "cat.jpg", psycopg2.Binary(drawing)))