我正在为qgis编写插件。在这个插件中,我需要查询数据库,并尝试使用pymysql。当我尝试在Qgis中安装插件时,消息错误为:ModuleNotFoundError:没有名为“ pymysql”的模块。 Pymsql已正确安装,并且通常可以正常运行,而且我确定在代码中导入了该模块。这是我使用数据库连接的代码部分。我的代码是否有问题,或者在Qgis插件中无法使用pymysql?
retrive = "SELECT DISTINCT ID FROM `beni` Join relazioni ON beni.ID = relazioni.IDR WHERE relazioni.data="+dataInizio
#database connection
connection = pymysql.connect(host="localhost",user="root",passwd="",database="mydb",port=3308 )
cursor = connection.cursor()
if connection.open:
#execute query
cursor.execute(retrive)
rows = cursor.fetchall()
if not rows:
self.iface.messageBar().pushMessage(
'Success', "rows is empty", level=Qgis.Success)
else:
#create Qgis Expression
seperator = ', '
rowsString=seperator.join(rows)
expr = QgsExpression( "id in ("+rowsString+")")
cLayer = iface.mapCanvas().currentLayer()
it = cLayer.getFeatures( QgsFeatureRequest( expr ) )
ids = [i.id() for i in it]
#select by ids and load style
cLayer.selectByIds(ids)
cLayer.loadNamedStyle(selectedStyle)
cLayer.triggerRepaint()
connection.close()