-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
240 lines (211 loc) · 7.29 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""Import modules"""
import csv
import sqlite3
import mysql.connector as maria
import data
import login
def create_database():
"""función crear database inventory"""
connection = maria.connect(**data.db_config_init)
try:
cursor = connection.cursor()
cursor.execute("""CREATE DATABASE IF NOT EXISTS inventory""")
except maria.Error as err:
print(err)
cursor.close()
connection.close()
def run_query(query, parameters=()):
"""función correr búsqueda"""
conn = None
cursor = None
try:
conn = maria.Connect(**data.db_config)
cursor = conn.cursor()
cursor.execute(query, parameters)
result = cursor.fetchall()
conn.commit()
return result
except maria.Error as err:
print(f"Error: {err}")
print(f"Consulta fallida: {cursor.statement}")
finally:
if cursor:
conn.close()
if conn:
cursor.close()
def run_query_sqlite3(query, parameters=()):
"""función correr búsqueda"""
conn = None
cursor = None
try:
conn = maria.Connect(**data.db_config)
cursor = conn.cursor()
cursor.execute(query, parameters)
result = cursor.fetchall()
conn.commit()
return result
except maria.Error as err:
print(f"Error: {err}")
print(f"Consulta fallida: {cursor.statement}")
finally:
if cursor:
conn.close()
if conn:
cursor.close()
def run_query_edit_sqlite3(query, parameters=()):
"""función correr edit sqlite3"""
conn = None
cursor = None
try:
conn = maria.Connect(**data.db_config)
cursor = conn.cursor()
cursor.execute(query, parameters)
conn.commit()
return 1
except maria.Error as err:
print(f"Error: {err}")
print(f"Cambio fallido: {cursor.statement}")
if err.errno == maria.errorcode.ER_DUP_ENTRY:
return 2
finally:
if cursor:
conn.close()
if conn:
cursor.close()
def run_query_edit(query, parameters=()):
"""función correr edit"""
conn = None
cursor = None
try:
conn = maria.Connect(**data.db_config)
cursor = conn.cursor()
cursor.execute(query, parameters)
conn.commit()
return 1
except maria.Error as err:
print(f"Error: {err}")
print(f"Cambio fallido: {cursor.statement}")
if err.errno == maria.errorcode.ER_DUP_ENTRY:
return 2
finally:
if cursor:
conn.close()
if conn:
cursor.close()
def create_table():
"""función crear tablas"""
connection = maria.Connect(**data.db_config)
try:
cursor = connection.cursor()
cursor.execute(
""" CREATE TABLE IF NOT EXISTS product (
id INT AUTO_INCREMENT PRIMARY KEY,
code VARCHAR(255) UNIQUE, name VARCHAR(255),
price_buy INT, ganancia INT, price_sales INT,
location INT, location2 INT, location3 INT) """)
connection.commit()
except maria.errors.IntegrityError as err:
print(err)
cursor.close()
connection.close()
def create_table_sales():
"""función crear tablas"""
connection = sqlite3.connect("sales.db")
try:
cursor = connection.cursor()
cursor.execute(
""" CREATE TABLE IF NOT EXISTS sales (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) UNIQUE, quantity INT, price INT) """
)
connection.commit()
except sqlite3.errors.IntegrityError as err:
print(err)
cursor.close()
connection.close()
def create_database_users():
"""función crear table users"""
connection = maria.Connect(**data.db_config)
try:
cursor = connection.cursor()
cursor.execute(
"""CREATE TABLE IF NOT EXISTS users(
id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) UNIQUE,
password VARCHAR(255))"""
)
connection.commit()
except maria.errors.IntegrityError as err:
print(err)
cursor.close()
connection.close()
def create_database_history():
"""función crear table history"""
connection = maria.Connect(**data.db_config)
try:
cursor = connection.cursor()
cursor.execute(
"""CREATE TABLE IF NOT EXISTS history(
id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), price INT,
quantity INT, user VARCHAR(255), action VARCHAR(255),
date VARCHAR(255))"""
)
connection.commit()
except maria.errors.IntegrityError as err:
print(err)
cursor.close()
connection.close()
def csv_import(file_name):
"""Función import cvs"""
with open(file_name, "r", encoding="utf-8") as csv_file:
lineas = len(csv_file.readlines())
csv_file.close()
with open(file_name, "r", encoding="utf-8") as csv_file:
reader = csv.reader(csv_file, delimiter=";")
f = 1
progreso = login.QtW.QProgressDialog(
"Importando Productos", None, 1, lineas)
progreso.show()
for row in reader:
query = """INSERT INTO product (code, name, price_buy, ganancia,
price_sales, location, location2, location3)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"""
price = round(float(row[2]) * (1 + (float(row[3]) / 100)))
parameters = (str(row[0].upper()), str(row[1].upper()),
round(float(row[2])), round(float(row[3])),
price, round(float(row[4])), round(float(row[5])),
round(float(row[6])))
result = run_query_edit(query, parameters)
if result == 2:
query_db_exist = """select price_buy, ganancia,
location, location2, location3, code from product"""
db_rows = run_query(query_db_exist)
for i in db_rows:
if i[5] == row[0]:
query = """UPDATE product SET price_buy=%s,
ganancia=%s, price_sales=%s, location=%s,
location2=%s, location3=%s WHERE code=%s"""
price_2 = round(float(row[2]) * (1 + (float(
row[3]) / 100)))
parameters = (row[2], row[3], price_2,
round(int(row[4])+int(i[2])),
round(int(row[5])+int(i[3])),
round(int(row[6])+int(i[4])), i[5])
run_query_edit(query, parameters)
progreso.setValue(f)
f += 1
csv_file.close()
progreso.close()
def csv_export(file_name):
"""Función export cvs"""
with open(file_name, "w", encoding="utf-8") as csv_file:
write = csv.writer(csv_file, delimiter=";")
query = """select code, name, price_buy, ganancia, location, location2,
location3 from product order by name desc"""
db_rows = run_query(query)
for x in db_rows:
write.writerow(x)
csv_file.close()
def change():
"Change datebase"
query = """DROP table product"""
run_query_edit(query)