-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSQL_DB.py
More file actions
32 lines (27 loc) · 1.08 KB
/
Copy pathPSQL_DB.py
File metadata and controls
32 lines (27 loc) · 1.08 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
import psycopg2
class PSQL_DB_Worker:
def __init__(self, host, user, password, database, port=5432):
self.connection = psycopg2.connect(
host=host, user=user, password=password, database=database, port=port
)
self.cursor = self.connection.cursor()
self.connection.autocommit = True
def create_table(self):
with self.connection:
self.cursor.execute(
"""CREATE TABLE IF NOT EXISTS ships(
id varchar,
Aircraft int[][],
Battleship int[][],
Cruiser int[][],
Submarine int[][],
Carrier int[][]);"""
)
def set_id(self, id):
with self.connection:
self.cursor.execute(f"""INSERT INTO ships VALUES ('{id}');""")
def update_coords(self, id, ship_name, positions):
with self.connection:
self.cursor.execute(
f"""UPDATE ships SET {ship_name}=ARRAY{positions} WHERE id='{id}';"""
)