简体   繁体   English

通过 Streamlit 向 PostgreSQL 表插入数据

[英]Inserting data to a PostgreSQL table through Streamlit

I'm trying to insert data from a Streamlit app to a table in a Postresql database.我正在尝试将 Streamlit 应用程序中的数据插入到 Postresql 数据库中的表中。 I have no problems whatsoever inserting the data into the table if I do so solely through my Python script ( I confirm that queries have been executed correctly by using pgAdmin4).如果我仅通过我的 Python 脚本(我确认使用 pgAdmin4 已正确执行查询)将数据插入表中,我将没有任何问题。

But whenever I execute the Streamlit button widget that sends the through the database the following error appears.但是,每当我执行通过数据库发送的 Streamlit 按钮小部件时,都会出现以下错误。

AttributeError: 'builtin_function_or_method' object has no attribute 'execute'

which refers to the cursor.execute (..) method指的是cursor.execute (..) 方法

Below is the script of the function that sends the data into the database through a st.button()下面是 function 的脚本,它通过 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)

i'd much appreciate any help!!我非常感谢任何帮助!

Try this...尝试这个...

# 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM