【发布时间】:2020-05-22 16:02:34
【问题描述】:
我在 Mac 上使用 PyQT5、Python3.7 和 Sqlite3 构建了我的第一个桌面应用程序。我正在使用 Windows 平台的 pyinstaller 创建 .exe(我已将代码移至 Windows,因为 Mac 上的 Wine 无法正常工作并且厌倦了 VM)。
当我尝试在 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')
不确定这是否是正确的方法。我对桌面应用程序开发非常陌生。寻找解决问题的正确方法。
【问题讨论】:
-
你能发布你的代码吗?
-
@EricJin 添加了代码供参考。
标签: python-3.x desktop-application