简体   繁体   中英

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

But whenever I execute the Streamlit button widget that sends the through the database the following error appears.

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

which refers to the cursor.execute (..) method

Below is the script of the function that sends the data into the database through a 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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