Answer the question
In order to leave comments, you need to log in
How to fix the database is locked error?
import os
import sqlite3
import sys
import random
import time
from PyQt5 import QtWidgets
import shop_design # Файл дизайна
import minutes_main_menu # Основной файл программы
import Weapon_handler # JSON с хар-ками оружия
import sqlite_logic # sqlite подключения, не относятся к этому файлу.
# IMPORTS#
class logic_shop(QtWidgets.QMainWindow, shop_design.Ui_MainWindow, QtWidgets.QLineEdit, QtWidgets.QListWidget):
def __init__(self):
try:
####
super(logic_shop, self).__init__()
self.setupUi(self)
print(1)
self.Exit_but.clicked.connect(lambda: self.exit_main())
## Подключение к pyqt ##
self.sqlite_close_weapon() # Закрытия на случай занятости БД (не помогает)
conn, cur, row = self.sqlite_connect_weapon() # Открытие нового подключения к БД
if row is None:
self.update_sqlite() # Обновление базы
time_update = 300
else:
if int(time.time()) - int(row[14]) >= 10: #Если с момента 1 подключения прошло больше 10 секунд
# Когда выполняется это условие - база становится закрытой.
self.update_sqlite()
print(1)
time_update = 300 - (int(time.time()) - int(row[14]))
else:
time_update = 300 - (int(time.time()) - int(row[14]))
self.sqlite_close_weapon() # Закрытие подключения
self.render_shop(time_update) # Подставление значений
print(1)
self.sqlite_close_weapon() # Закрытие подключения
except Exception as ex:
print(ex)
def render_shop(self, time_update):
conn, cur, row = self.sqlite_connect_weapon()
####
self.label_2.setText(row[1])
self.label_9.setText(row[4])
self.label_10.setText(row[7])
self.label_11.setText(row[10])
for i in Weapon_handler.data['weapon']['swords']:
if i['name'] == row[10]:
self.label_12.setText(f"{i['damage']} ATK")
self.label_16.setText(f"{i['speed']} SPD")
self.label_20.setText(row[11])
self.label_24.setText(i['element'])
self.label_35.setText(f"{row[3]} EUTS")
if i['name'] == row[7]:
self.label_13.setText(f"{i['damage']} ATK")
self.label_17.setText(f"{i['speed']} SPD")
self.label_21.setText(row[8])
self.label_25.setText(i['element'])
self.label_36.setText(f"{row[6]} EUTS")
if i['name'] == row[4]:
self.label_14.setText(f"{i['damage']} ATK")
self.label_18.setText(f"{i['speed']} SPD")
self.label_22.setText(row[5])
self.label_26.setText(i['element'])
self.label_37.setText(f"{row[9]} EUTS")
if i['name'] == row[1]:
self.label_15.setText(f"{i['damage']} ATK")
self.label_19.setText(f"{i['speed']} SPD")
self.label_23.setText(row[2])
self.label_27.setText(i['element'])
self.label_38.setText(f"{row[12]} EUTS")
self.label_28.setText('NO INFORMATION')
self.label_29.setText('NO INFORMATION')
self.label_30.setText('NO INFROMATION')
self.label_31.setText('NO INFORMATION')
self.label_33.setText(str(row[13]))
self.Timer.setText(f'{time_update} SEC')
### Просто подставление значений ###
self.sqlite_close_weapon()
def exit_main(self):
self.destroy()
print('Выполняю выход')
minutes_main_menu.main_table().shop_open.setEnabled(True)
# Выход в основной файл
def sqlite_connect_weapon(self):
conn = sqlite3.connect(f'C:/Users/{os.getlogin()}/appdata/Roaming/5min.db', timeout=1) # Подключение к файлу базы
cur = conn.cursor()
cur.execute('SELECT * FROM SHOP_ACTIVE')
row = cur.fetchone()
return conn, cur, row
def sqlite_close_weapon(self):
conn, cur, row = self.sqlite_connect_weapon()
conn.commit()
cur.close()
conn.close() # Закрытие подключений
def update_sqlite(self):
conn, cur, row = self.sqlite_connect_weapon()
cur.execute('DELETE FROM SHOP_ACTIVE') # Очистка базы на всякий случай, дабы не было дублирования
login = sqlite_logic.give_login()[0] # Получение логина аккаунта
self.sqlite_close_weapon()
sqlite_logic.close_connection()
# Закрытие обоих баз данных.
first_weapon, second_weapon, three_weapon, fourble_weapon = Weapon_handler.data['weapon']['swords'][
self.get_int(0)], \
Weapon_handler.data['weapon']['swords'][
self.get_int(0)], \
Weapon_handler.data['weapon']['swords'][
self.get_int(0)], \
Weapon_handler.data['weapon']['swords'][
self.get_int(0)]
# Получение из JSON названия по рандому
first_enchant, second_enchant, three_enchant, fourble_enchant = Weapon_handler.data['enchant'][
self.get_int(1)], \
Weapon_handler.data['enchant'][
self.get_int(1)], \
Weapon_handler.data['enchant'][
self.get_int(1)], \
Weapon_handler.data['enchant'][
self.get_int(1)]
# Получение из JSON энчантов по рандому
first_price, second_price, three_price, fourble_price = random.randint(7, 77) * first_enchant[
0], random.randint(7, 77) * second_enchant[0], random.randint(7, 77) * three_enchant[0], random.randint(
7, 77) * fourble_enchant[0]
# Рандомная цена
if first_enchant[2] == 'Mythic':
first_price = int(first_price) * 4
if second_enchant[2] == 'Mythic':
second_price = int(second_price) * 4
if second_enchant[2] == 'Mythic':
three_price = int(three_price) * 4
if second_enchant[2] == 'Mythic':
fourble_price = int(fourble_price) * 4
# Умножение цен если мифик оружие
heals = random.randint(0, 20)
# Кол-во хилок на продажу
time_start = time.time()
# Начало отсчета для 30 строки
price_list = (
login, first_weapon['name'], first_enchant[2], first_price, second_weapon['name'], second_enchant[2],
second_price, three_weapon['name'], three_enchant[2], three_price, fourble_weapon['name'],
fourble_enchant[2], fourble_price, heals, time_start)
# Список для вноса в базу данных
conn, cur, row = self.sqlite_connect_weapon()
cur.execute('insert into SHOP_ACTIVE values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', price_list)
conn.commit()
#Внос
self.sqlite_close_weapon()
def get_int(self, query):
## Рандом для оружия ##
if query == 0:
return random.randint(0, 16)
if query == 1:
integ = random.randint(0, 4)
if integ == 0:
return 'simple'
if integ == 1:
return 'rare'
if integ == 2:
return 'epic'
if integ == 3:
return 'legendary'
if integ == 4:
return 'mythic'
## Основной рендер окон ###
class windows_render:
def __init__(self):
self.windows = [logic_shop]
def render_main_windows(self):
app = QtWidgets.QApplication(sys.argv)
self.windows[0]().show()
app.exec_()
if __name__ == '__main__':
windows_render().render_main_windows()
Answer the question
In order to leave comments, you need to log in
Solved a problem. Carried out insert for function, helped. Crutch of course..
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question