-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresql.py
More file actions
148 lines (112 loc) · 4.09 KB
/
Copy pathpostgresql.py
File metadata and controls
148 lines (112 loc) · 4.09 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python
import psycopg2
from config import config
def createTable(query):
connection = None
try:
params = config()
# connect to the PostGreSQL Server
print('Connecting to the PostGreSQL Database Server...')
connection = psycopg2.connect(**params)
# Create a cursor
cursor = connection.cursor()
# Inserting a row into the mobile table
cursor.execute(query)
connection.commit()
print('Table has been created')
cursor.close()
finally:
if connection is not None:
connection.close()
print('Database connection closed.')
def insertRow(id, name, price):
connection = None
try:
params = config()
# connect to the PostGreSQL Server
print('Connecting to the PostGreSQL Database Server...')
connection = psycopg2.connect(**params)
# Create a cursor
cursor = connection.cursor()
# Inserting a row into the mobile table
cursor.execute('INSERT INTO mobile (ID, MODEL, PRICE) VALUES (%s,%s,%s)',
(id, name, price)
)
connection.commit()
print('Row with id', id, 'has been inserted')
cursor.close()
finally:
if connection is not None:
connection.close()
print('Database connection closed.')
def readTable(tableName):
connection = None
try:
params = config()
# connect to the PostGreSQL Server
print('Connecting to the PostGreSQL Database Server...')
connection = psycopg2.connect(**params)
# Create a cursor
cursor = connection.cursor()
postgres_read_query = "SELECT * FROM " + tableName
cursor.execute(postgres_read_query)
# Reading from the table
print("Selecting rows from mobile table using cursor.fetchall")
mobile_records = cursor.fetchall()
print("Print each row and it's columns values")
for row in mobile_records:
print("ID =", row[0])
print("Model =", row[1])
print("Price =", row[2], "\n")
cursor.close()
finally:
if connection is not None:
connection.close()
print('Database connection closed.')
def updateRow(id, price):
connection = None
try:
params = config()
# connect to the PostGreSQL Server
print('Connecting to the PostGreSQL Database Server...')
connection = psycopg2.connect(**params)
# Create a cursor
cursor = connection.cursor()
# Update the table
postgres_update_query = "UPDATE mobile SET price = %s WHERE id = %s"
cursor.execute(postgres_update_query, (price, id))
connection.commit()
print('Row with id', id, 'has been updated')
cursor.close()
finally:
if connection is not None:
connection.close()
print('Database connection closed.')
def deleteRow(id):
connection = None
try:
params = config()
# connect to the PostGreSQL Server
print('Connecting to the PostGreSQL Database Server...')
connection = psycopg2.connect(**params)
# Create a cursor
cursor = connection.cursor()
# Update the table
postgres_delete_query = "DELETE from mobile WHERE id = %s"
cursor.execute(postgres_delete_query, (id, ))
connection.commit()
print('Row with id', id, 'has been deleted')
cursor.close()
finally:
if connection is not None:
connection.close()
print('Database connection closed.')
if __name__ == '__main__':
# createTable('''CREATE TABLE mobile
# (ID INT PRIMARY KEY NOT NULL,
# MODEL TEXT NOT NULL,
# PRICE REAL); ''')
# insertRow(10, 'Google Pixel', 1000)
# updateRow(10, 1100)
# deleteRow(10)
readTable("mobile")