-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
58 lines (41 loc) · 1.35 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import psycopg2
import psycopg2.extras
import os
username = os.environ['USERNAME']
pwd = os.environ['EMAIL_USER_PASSWORD']
hostname = os.environ['HOSTNAME']
port_id = os.environ['PORT_ID']
database = os.environ['DATABASE']
db = None
date = None
try:
db = psycopg2.connect(
host = hostname,
dbname = database,
user = username,
password = pwd,
port = port_id)
date = db.cursor(cursor_factory=psycopg2.extras.DictCursor)
print("Connected to Postgresql")
create_table = ''' CREATE TABLE IF NOT EXISTS telegram_ari (
user_id serial PRIMARY KEY,
ID VARCHAR (50) UNIQUE NOT NULL,
email VARCHAR (50) NOT NULL) '''
date.execute(create_table)
sqlite_select_query = """SELECT * from telegram_ari"""
date.execute(sqlite_select_query)
postgresql_date = date.fetchall()
print("Total rows are: ", len(postgresql_date))
print("Printing each row")
for row in postgresql_date:
Identificationnumber = row['ID']
email = row['email']
db.commit()
except Exception as error:
print(print("Failed to read data from table", error))
finally:
if date is not None:
date.close()
if db is not None:
db.close()
print("The Sqlite connection is closed")