我有两个问题 - 第一次关闭(更重要的是)第二次从QGIS Python运行代码时它失败了“线程试图读取或写入虚拟地址但没有相应的访问”。
第二个问题是,如果我关闭并删除数据库,使用projectView.show()显示的表是空的,如果我将这些行注释掉,数据会正确显示。
from PyQt4.QtSql import *
from PyQt4.QtGui import *
from qgis.gui import *
db = QSqlDatabase.addDatabase('QODBC')
if db.isValid():
db.setDatabaseName('DRIVER={SQL Server};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s;'
% ('ServerName',
'Database',
'username',
'password'))
#print "db.isValid"
if db.open():
#print "success"
query = db.exec_("""SELECT TOP 100 postal_address as address, EASTING as x, NORTHING as y FROM dbo.POSTAL_ADDRESS WHERE postal_address LIKE('%EX3 %')""")
projectModel = QSqlQueryModel()
projectModel.setQuery(query)
projectView = QTableView()
projectView.setModel(projectModel)
projectView.show()
QMessageBox.warning(None, "Info", "hello")
query = db.exec_("""SELECT TOP 100 postal_address as address, EASTING as x, NORTHING as y FROM dbo.POSTAL_ADDRESS WHERE postal_address LIKE('%EX2 %')""")
projectModel = QSqlQueryModel()
projectModel.setQuery(query)
projectView = QTableView()
projectView.setModel(projectModel)
projectView.show()
#db.close()
#db.removeDatabase('QODBC')
else:
QMessageBox.warning(None, "Info", "Error")
else:
QMessageBox.warning(None, "Info", "Error")