I would like to insert multiple rows in PostgreSQL table. I attached the program. I used a list of items: new_record = ['one','two','three']. Actually, this list will be filled with 100000 items from Amazon products but now I fill with 3 items. I make a for loop to iterate but it filled with only one element. I would like to print all the items. For example:
"INSERT INTO products(products_amazon) VALUES ('one')"
"INSERT INTO products(products_amazon) VALUES ('two')"
"INSERT INTO products(products_amazon) VALUES ('three')"
I will appreciate any help.
import psycopg2
from pprint import pprint
database = "amazon_products"
host = "localhost"
user = "postgres"
password = "123QQsuccess"
class DatabaseConnection:
def __init__(self):
try:
self.connection = psycopg2.connect(database=database, host=host, user=user, password=password)
self.connection.autocommit = True
self.cursor = self.connection.cursor()
except:
pprint('Cannot connect to database')
def insert_new_record(self):
new_record = ['one','two','three']
for item in new_record:
insert_command = "INSERT INTO products(products_amazon) VALUES ('" + item + "')"
pprint(insert_command)
self.cursor.execute(insert_command)
if __name__ == '__main__':
database_connection = DatabaseConnection()
database_connection.insert_new_record()