import psycopg2
import sys
import datetime
from psycopg2 import Error
try:
db = psycopg2.connect(user="postgres",password="*",host="127.0.0.1",port="5432")
cursor = db.cursor()
except (Exception, Error) as e:
print('error: ', e)
sys.exit()
try:
cursor.execute('CREATE DATABASE private_db')
except Error :
print('error: ', Error)
db.rollback()
cursor.execute('''CREATE TABLE IF NOT EXISTS test_table (
bool_data BOOL,
float_data REAL,
int_data BIGINT,
str_data TEXT,
datetime_data TIMESTAMP
)''')
db.commit()
cursor.execute('INSERT INTO test_table VALUES (%s, %s, %s, %s, %s)', (True, 1.1, 1, 'sd', datetime.datetime.now()))
db.commit()
cursor.execute("SELECT * FROM test_table WHERE bool_data = %s", [True])
result = cursor.fetchall()
print(result)
cursor.execute('UPDATE test_table SET int_data = %s WHERE bool_data = %s', (2, False))
db.commit()
cursor.execute('DELETE FROM test_table WHERE int_data = %s', [1])
db.commit()
cursor.close()
db.close()