insert into postgres python

Solutions on MaxInterview for insert into postgres python by the best coders in the world

showing results for - "insert into postgres python"
Amari
08 May 2016
1
2        
3            
4        
5     #!/usr/bin/python
6
7import psycopg2
8from config import config
9
10
11def insert_vendor(vendor_name):
12    """ insert a new vendor into the vendors table """
13    sql = """INSERT INTO vendors(vendor_name)
14             VALUES(%s) RETURNING vendor_id;"""
15    conn = None
16    vendor_id = None
17    try:
18        # read database configuration
19        params = config()
20        # connect to the PostgreSQL database
21        conn = psycopg2.connect(**params)
22        # create a new cursor
23        cur = conn.cursor()
24        # execute the INSERT statement
25        cur.execute(sql, (vendor_name,))
26        # get the generated id back
27        vendor_id = cur.fetchone()[0]
28        # commit the changes to the database
29        conn.commit()
30        # close communication with the database
31        cur.close()
32    except (Exception, psycopg2.DatabaseError) as error:
33        print(error)
34    finally:
35        if conn is not None:
36            conn.close()
37
38    return vendor_id
Rose
26 Feb 2018
1
2        
3            
4        
5     def insert_vendor_list(vendor_list):
6    """ insert multiple vendors into the vendors table  """
7    sql = "INSERT INTO vendors(vendor_name) VALUES(%s)"
8    conn = None
9    try:
10        # read database configuration
11        params = config()
12        # connect to the PostgreSQL database
13        conn = psycopg2.connect(**params)
14        # create a new cursor
15        cur = conn.cursor()
16        # execute the INSERT statement
17        cur.executemany(sql,vendor_list)
18        # commit the changes to the database
19        conn.commit()
20        # close communication with the database
21        cur.close()
22    except (Exception, psycopg2.DatabaseError) as error:
23        print(error)
24    finally:
25        if conn is not None:
26            conn.close()
queries leading to this page
insert data in postgresql using pythonpython postgresql insertpython insert into postgresql databasehow to connect with postgres table and insert data in python python insert in postgrespostgresql python insert datapython postgresql insert commandinsert into postgresql from pythoninsert into postgres from pythonhow to insert data in postgres tableinsert data postgres pythonhow to insert values into table in postgresql using pythonpython postgres insert datapostgres in python insertpostgresql insert pythoninsert into postgresql database pythoninsert in postgres pythoninsert into postgres pythonpython insert to postgres tablehow to insert values in a table in postgresql ussing pythoninsert data into postgres table using pythonpython insert into postgresql examplehow to save table from python to postgresqlpostgresql python insertinsert data in table in postgresql using pythoninsert postgres pythonpython connect to postgresql and insertpython script to insert data into postgresqlpython postgresql insert into schemapython code to insert data into postgresqlhow to push data to databse python psycopg2insert into postgresqlhow to insert in data in postgress use pythoninsert data postgresql pythonpostgresql insert into table 22python 22python postgresql insert querypython insert postgresqlhow to add data to postgresql database with pythonpysthon postgres insertinsert data in postgresql pythonpython insert to postgresqlpython run script to insert data into postgrespostgres python insertinsert in postgresql pythnadd an item to table in python postgres 3fpostgres insert pythonpython postgres inserthow to insert values in postgresql table using pythoninsert python postgresinsert python list into postgresinsert data into postgresql using pythonpython insert into psqlinsert python list in postgresqlinsert query of postgresql in pythonhow to insert data into postgres table using pythoninsert into postgresql pythonpython insert into postgresinsert data into postgresql pythonpandas insert dataframe to postgrespython postgrsql insert intopython postgresql execute insertinsert data to postgresql from code in pythonpostgres insert query with pythonpostgresql python insert data to tableinsert data to postgresql pythoninsert data into postgres using pythonhow to insert data into postgresql using pythoninsert postgresql pythoninsert into postgres python