我在Mac上使用PyQT5、Python3.7和Sqlite3构建了我的第一个桌面应用程序。我正在使用pyinstaller为windows平台创建.exe (我已经把我的代码移到了windows上,因为Mac上的Wine不能正常工作,而且厌倦了虚拟机)。
当我移动我的代码后,当我试图在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')
不知道这是否是正确的方法。我对桌面应用开发很陌生。寻找正确的方法来解决这个问题。
我强烈建议你把你的代码和你的问题一起发布。它有助于我们更好地回答问题。
要把代码放在问题中,请在````和````中包含它。
编辑--我不能添加评论,因为我还没有50个信誉,所以很抱歉。