【发布时间】:2020-10-31 11:07:00
【问题描述】:
我正在尝试将 Streamlit 应用程序中的数据插入到 Postresql 数据库中的表中。如果我仅通过我的 Python 脚本将数据插入表中,我将没有任何问题(我确认已使用 pgAdmin4 正确执行了查询)。
但每当我执行通过数据库发送的 Streamlit 按钮小部件时,就会出现以下错误。
AttributeError: 'builtin_function_or_method' object has no attribute 'execute'
指的是cursor.execute (..) 方法
下面是通过st.button()将数据发送到数据库的函数脚本
import psycopg2
import pandas as pd
import streamlit as st
@st.cache(allow_output_mutation=True)
def insert_record2(tuple):
# Connect to the PostgreSQL database server
conn = psycopg2.connect(host='localhost',
port='5432',
database='xxxy',
user= 'postgres',
password= 'xxxx')
conn.autocommit = True
sql = """INSERT INTO main_fkl_mini_2 (project_name ,sponsor1 ,sponsor2 ,sponsorsh1 ,sponsorsh2 ) VALUES (%s,%s,%s,%s,%s)"""
cursor = conn.cursor()
cursor.execute(sql, tuple)
insert_record2(tuplex)
非常感谢任何帮助!
【问题讨论】:
标签: python postgresql streamlit