我已经在Mac上使用PyQT5,Python3.7和Sqlite3构建了我的第一个桌面应用程序。我正在使用pyinstaller在Windows平台上创建.exe(由于Mac上的Wine无法正常工作并且对VM感到厌倦,所以我已将代码移至Windows。)
当我尝试在Windows上运行该应用程序时移动了代码后,由于sqlite3数据库没有表而导致崩溃。 .exe也将发生这种情况。
我可以编写一个实用程序函数,以在应用启动前检查所有表是否存在,并创建表(如下所示)是否不存在。
def tables_exist():
dbc = create_connection().cursor()
dbc.execute("SELECT name FROM sqlite_master WHERE type='table'")
res = dbc.fetchall()
tables = {r[0] for r in res}
if len(TABLES_SET^tables) == 0: # TABLES_SET is a set of all the table names
return True
else:
try:
scriptFile = open(DB_DEFINATION, 'r')
script = scriptFile.read()
scriptFile.close()
dbc.executescript(script)
dbc.connection.commit()
print('Table Created')
return True
except:
print("Something went wrong:")
return False
finally:
dbc.close()
在我的app.py中调用上述方法:
def db_check(self):
if tables_exist():
return True
else:
show_maessage_box(self, message='Could not create tables.')
print('Backend no supported!')
return False
app =QApplication(sys.argv)
main = Main() # This is a custom class containing main PyQt Dialogbox
if db_check():
main.show()
app.exec()
else:
print('Something went wrong')
不确定这是否正确。我是桌面应用程序开发的新手。寻找解决问题的正确方法。
答案 0 :(得分:0)
我强烈建议您将代码与问题一起发布。它有助于我们更好地回答问题。
要将代码放入问题中,请将其包含在```和````中。
编辑-由于我尚无50个声望,因此我无法添加评论,对此感到抱歉。