我在 python 中编写代码,我需要用数据库选择来填写一个表,并且每 5 分钟刷新一次,有人可以帮我吗?我已经尝试了很多方法,但没有一个是正确的。
有什么建议吗?
MainWindow >
def createConnection():
connString = "host='*********' port='****' dbname='***' user='***' password='*******'"
global db
db = QSqlDatabase.addDatabase('QPSQL')
db.setDatabaseName(connString)
if db.open():
print('connect to SQL Server successfully')
return True
else:
print('connection failed')
return False
def displayData(sqlStatement):
print('processing query...')
qry = QSqlQuery(db)
qry.prepare(sqlStatement)
qry.exec()
model = QSqlQueryModel()
model.setQuery(qry)
view = QTableView()
view.setModel(model)
return view
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.showMaximized()
if createConnection():
SQL_STATEMENT = "select prod_descricao,prun_preco from produnidade inner join produtos ON (prod_codigo = prun_prod_codigo) where prun_unid_codigo = '101' limit 20"
dataView = displayData(SQL_STATEMENT)
dataView.show()
sys.exit(app.exec_())