insert into postgres python


        
            
        
     #!/usr/bin/python

import psycopg2
from config import config


def insert_vendor(vendor_name):
    """ insert a new vendor into the vendors table """
    sql = """INSERT INTO vendors(vendor_name)
             VALUES(%s) RETURNING vendor_id;"""
    conn = None
    vendor_id = None
    try:
        # read database configuration
        params = config()
        # connect to the PostgreSQL database
        conn = psycopg2.connect(**params)
        # create a new cursor
        cur = conn.cursor()
        # execute the INSERT statement
        cur.execute(sql, (vendor_name,))
        # get the generated id back
        vendor_id = cur.fetchone()[0]
        # commit the changes to the database
        conn.commit()
        # close communication with the database
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

    return vendor_id

4.2
5
Vbif212 105 points

                                    
        
            
        
     def insert_vendor_list(vendor_list):
    """ insert multiple vendors into the vendors table  """
    sql = "INSERT INTO vendors(vendor_name) VALUES(%s)"
    conn = None
    try:
        # read database configuration
        params = config()
        # connect to the PostgreSQL database
        conn = psycopg2.connect(**params)
        # create a new cursor
        cur = conn.cursor()
        # execute the INSERT statement
        cur.executemany(sql,vendor_list)
        # commit the changes to the database
        conn.commit()
        # close communication with the database
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

4.2 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to insert in data in postgress use python python insert to postgresql pysthon postgres insert python connect to postgresql and insert insert data into postgres using python python postgrsql insert into insert data to postgresql from code in python postgres insert python insert into postgres from python postgres in python insert Insert data into Postgres table using Python insert data postgres python postgresql python insert data postgres insert query with python how to insert values into table in postgresql using python insert data into postgresql using python postgresql python insert data to table insert data into postgresql python python postgres insert data python insert into psql python postgresql insert python insert into postgresql example insert data in postgresql python python postgresql insert command how to insert data into postgresql using python insert into postgresql python insert postgres python insert into postgresql from python python insert to postgres table postgresql python insert python insert into postgres postgresql insert into table "python" insert python list in postgresql postgresql insert python how to connect with postgres table and insert data in python insert data in table in postgresql using python insert in postgres python python script to insert data into postgresql pandas insert dataframe to postgres insert python postgres python postgresql insert into schema insert postgresql python python code to insert data into postgresql insert query of postgresql in python python insert postgresql python insert into postgresql database insert in postgresql pythn how to save table from python to postgresql add an item to table in python postgres? insert into postgres python how to push data to databse python psycopg2 how to add data to postgresql database with python how to insert data in postgres table insert into postgresql postgres python insert python run script to insert data into postgres python postgres insert insert data to postgresql python python postgresql insert query
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source