Я пытаюсь подключиться SQL База данных сервера. Вот мой код:
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
from PyQt5.QtWidgets import QTableView, QApplication
import sys
SERVER = '192.19.45.11:1433'
DATABASE = 'insightAccounts'
USERNAME = 'Admin'
PASSWORD = '******'
db = QSqlDatabase.addDatabase('QODBC')
db.setDatabaseName(f'Driver={{SQL SERVER}}; Server={SERVER}; Database={DATABASE}; UID={USERNAME}; PWD={PASSWORD}')
db.open()
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__':
app = QApplication(sys.argv)
SQL_STATEMENT = 'SELECT top 5 * FROM insightAccounts.dbo.insightAccount'
dataView = displayData(SQL_STATEMENT)
dataView.show()
app.exit()
sys.exit(app.exec_())
Вот вывод:
processing query...
QSqlQuery::prepare: database not open
"QODBCResult::exec: No statement handle available" Error: ""