Answer the question
In order to leave comments, you need to log in
How to open all databases in postgresql?
Why doesn't it show new tables created from psycopg2?
Code example (where there are all the basic actions with the table)
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()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question