-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
55 lines (51 loc) · 2.87 KB
/
main.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
# импортируемый необходимые библиотеки
import sqlite3
import openpyxl
# создаем базу данных
conn = sqlite3.connect('URSiP.db')
# создаем объект курсора для работы с базой данных
cursor = conn.cursor()
# создаем таблицу
cursor.execute('''CREATE TABLE IF NOT EXISTS mytable (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company TEXT,
fact_qliq_data1 INTEGER,
fact_qliq_data2 INTEGER,
fact_qoil_data1 INTEGER,
fact_qoil_data2 INTEGER,
forecast_qliq_data1 INTEGER,
forecast_qliq_data2 INTEGER,
forecast_qoil_data1 INTEGER,
forecast_qoil_data2 INTEGER)''')
# открываем xlsx файл
book = openpyxl.open("attachment.xlsx", read_only=True)
sheet = book.active
# читаем данные из таблицы
for row in range(4, sheet.max_row + 1):
company = sheet[row][1].value
fact_qliq_data1 = int(sheet[row][2].value)
fact_qliq_data2 = int(sheet[row][3].value)
fact_qoil_data1 = int(sheet[row][4].value)
fact_qoil_data2 = int(sheet[row][5].value)
forecast_qliq_data1 = int(sheet[row][6].value)
forecast_qliq_data2 = int(sheet[row][7].value)
forecast_qoil_data1 = int(sheet[row][8].value)
forecast_qoil_data2 = int(sheet[row][9].value)
# записываем данные из таблицы в базу данных
cursor.execute("INSERT INTO mytable (company, fact_qliq_data1, fact_qliq_data2, fact_qoil_data1, \
fact_qoil_data2, forecast_qliq_data1,forecast_qliq_data2, forecast_qoil_data1, forecast_qoil_data2) \
VALUES (?,?,?,?,?,?,?,?,?)", (company, fact_qliq_data1, fact_qliq_data2, fact_qoil_data1,
fact_qoil_data2, forecast_qliq_data1, forecast_qliq_data2, forecast_qoil_data1,
forecast_qoil_data2))
# считаем total по каждой колонке и сохраняем в список totals
totals = cursor.execute("SELECT SUM(fact_qliq_data1), SUM(fact_qliq_data2), SUM(fact_qoil_data1), \
SUM(fact_qoil_data2), SUM(forecast_qliq_data1), SUM(forecast_qliq_data2), \
SUM(forecast_qoil_data1), SUM(forecast_qoil_data2) FROM mytable").fetchone()
# добавляем новую строку с полученными суммами в таблицу
cursor.execute("INSERT INTO mytable (company, fact_qliq_data1, fact_qliq_data2, fact_qoil_data1, \
fact_qoil_data2, forecast_qliq_data1, forecast_qliq_data2, forecast_qoil_data1, \
forecast_qoil_data2) VALUES ('total', ?, ?, ?, ?, ?, ?, ?, ?)",
totals)
# сохраняем изменения и закрываем подключение
conn.commit()
conn.close()