from postgres_example.config import connection, cursor, config # Добавление одного кортежа данных sql = f"INSERT INTO {config.TABLE} (name, phone, email, adress) VALUES (%s, %s, %s, %s)" values = ('Test1', '8999999', '*****@*****.**', 'Moscow') cursor.execute(sql, values) connection.commit() values = [ ("Vasya1", "89009009999", None, "Moscow"), ("Vasya2", "89009009999", None, "Minsk"), ("Vasya3", "89009009999", None, "Vladivostok"), ("Vasya5", "89009009999", None, "Berlin"), ] cursor.executemany(sql, values) connection.commit() cursor.close() connection.close()
from postgres_example.config import connection, cursor TABLE = "contacts" # Execute delete data query cursor.execute(f"DELETE FROM {TABLE} WHERE name='Test'") connection.commit() # CLosing connections cursor.close() connection.close()
from postgres_example.config import connection, cursor, config # Выполняем запрос cursor.execute(f"SELECT * FROM {config.TABLE}") # Получаем данные в виде кортежей for row in cursor.fetchall(): print(row) # Закрываемся cursor.close() connection.close()
import psycopg2 from postgres_example.config import connection, cursor try: with open("contacts_table.sql", "r") as script: cursor.execute(script.read()) except psycopg2.errors.DuplicateTable as e: print(e) cursor.close() connection.close()
from postgres_example.config import connection, cursor, config # Executing update cursor.execute(f"UPDATE {config.TABLE} SET name = %s WHERE name = %s", ("TEST", "Vasya")) connection.commit() # Altering table cursor.execute(f"ALTER TABLE {config.TABLE} DROP address") connection.commit() # Close connection cursor.close() connection.close()
from postgres_example.config import connection, cursor # Execute delete data query cursor.execute("DELETE FROM contacts_app WHERE name='Test'") connection.commit() # CLosing connections cursor.close() connection.close()