-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_lib.py
124 lines (81 loc) · 3.56 KB
/
sql_lib.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
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
import sqlite3
from PyQt5.QtSql import (QSqlDatabase, QSqlQuery, QSqlTableModel,
QSqlRelationalTableModel, QSqlRelation)
def sql_put_and_get_row_id(sql_command):
con = sqlite3.connect('sql_db.db')
cur = con.cursor()
cur.execute(sql_command)
output = cur.lastrowid
con.commit()
con.close()
return output
def sql_set(sql_command):
sql_put(sql_command) # To avoid some errors
def sql_put(sql_command):
con = sqlite3.connect('sql_db.db')
cur = con.cursor()
cur.execute(sql_command)
con.commit()
con.close()
def sql_get(sql_command):
con = sqlite3.connect('sql_db.db')
cur = con.cursor()
cur.execute(sql_command)
output = cur.fetchone()
con.close()
return output
def sql_get_all(sql_command):
con = sqlite3.connect('sql_db.db')
cur = con.cursor()
cur.execute(sql_command)
output = cur.fetchall()
con.close()
return output
def sql_get_all_cities_in_district(district):
return sql_get_all(f'SELECT * FROM `cities` WHERE district = "{district}";')
def sql_get_all_districts(region):
temp = sql_get_all(f'SELECT DISTINCT district FROM `cities` WHERE region = "{region}";')
result = ["Выберите район"]
for elem in temp:
result.append(elem[0])
# print(result)
return result
def sql_get_city_enabled_status(city):
return sql_get(f'SELECT enabled FROM `cities` WHERE name = "{city}";')[0]
def sql_get_all_regions():
temp = sql_get_all(f'SELECT DISTINCT region FROM `cities`;')
result = ['Выберите регион']
for elem in temp:
result.append(elem[0])
return result
def sql_set_city_enable_status(city):
sql_set(f'UPDATE `cities` SET enabled = "True" WHERE name = "{city}";')
def sql_set_city_disable_status(city):
sql_set(f'UPDATE `cities` SET enabled = "False" WHERE name = "{city}";')
def sql_get_all_enabled_cities(district):
return sql_get_all(f'SELECT * FROM `cities` WHERE enabled = "True" AND district = "{district}";')
def sql_get_helis_names():
temp = sql_get_all(f'SELECT name FROM `helicopters`;')
result = ['Выберите вертолёт']
for elem in temp:
result.append(elem[0])
return result
def sql_get_cur_heli(heli):
return sql_get_all(f'SELECT * FROM `helicopters` WHERE name = "{heli}";')
def sql_get_helis_all():
return sql_get_all(f'SELECT * FROM `helicopters`;')
# DATABASE #############################################################################################################
def sql_get_columns(table):
return sql_get_all(f"PRAGMA table_info(`{table}`);")
def sql_update_data(table, column, data, name):
query = f'UPDATE `{table}` SET "{column}" = "{data}" WHERE name = "{name}";'
print(query)
sql_put(f'UPDATE `{table}` SET "{column}" = "{data}" WHERE name = "{name}";')
def sql_get_previous_data(table, column, name):
return sql_get(f'SELECT "{column}" FROM `{table}` WHERE name = "{name}";')
def sql_delete_row(table, name):
sql_set(f'DELETE FROM `{table}` WHERE name = "{name}";')
def sql_cities_add_empty_row():
sql_set(f'INSERT INTO `cities` (name, lat, lon, region, district, enabled) VALUES ("Город", 0.0, 0.0, "Регион", "Район", True);')
def sql_helicopters_add_empty_row():
sql_set(f'INSERT INTO `helicopters` (name, picture, cruise_speed, max_distance, mass_empty, mass_full, fuel_consum, time_engine, cost_service, cost_crew, cost_fuel, koef_gsm, koef_diff, c_aeno) VALUES ("Название", "None", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);')