【问题标题】:Inserting data to a PostgreSQL table through Streamlit通过 Streamlit 向 PostgreSQL 表中插入数据
【发布时间】: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


    【解决方案1】:

    试试这个...

    # 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 {} VALUES """.format(tuple)
    
    cursor = conn.cursor()
    cursor.execute(sql, tuple)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-12
      • 2015-08-16
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多