报错信息:
# 未获取异常如:
AttributeError: 'str' object has no attribute 'cursor'
# 获取异常如:
__init__() takes 1 positional argument but 5 were given
本地使用pymysql的场景:使用了tkinter进行GUI开发功能,又采用了pyinstaller进行了打包成exe的操作。
了解tkinter常用组件:https://blog.csdn.net/yingshukun/article/details/78705337
组件使用情况,其他是python自带的
pip3 install PyInstaller
pip3 install paramiko
pip3 install pymysql
原本是在python365的环境上进行的操作,历史中都没有出现打包及链接数据库问题
换了新的电脑,python环境升级到了python370,发生了链接数据问题,即最前边提到的报错,
以下对比python365和python370链接数据库的代码变化
可以发现如下差异,不同点在于pymysql.connect的参数传递存在差异,python370严格限制了必须要传递对应的参数名称,以防止顺序错误。
def __init__(self, selectdb):
if (selectdb == 1):
self.host = "xxxxxx"
self.username = "xxxxxx"
self.password = "xxxxxx"
self.dbname = "xxxxxx"
else:
self.host = "xxxxxx"
self.username = "xxxxxx"
self.password = "xxxxxx"
self.dbname = "xxxxxx"
self.selectdb = selectdb
try:
self.db = pymysql.connect(self.host, self.username, self.password, self.dbname)
except Exception as e:
# 上传完毕弹出完成框
tkinter.messagebox.showerror("定制封面应用脚本", "连接数据库失败")
self.cursor = self.db.cursor()
def __init__(self, selectdb):
if (selectdb == 1):
self.host = "xxxxxx"
self.username = "xxxxxx"
self.password = "xxxxxx"
self.dbname = "xxxxxx"
else:
self.host = "xxxxxx"
self.username = "xxxxxx"
self.password = "xxxxxx"
self.dbname = "xxxxxx"
self.selectdb = selectdb
try:
self.db = pymysql.connect(host=self.host, port=3306, user=self.username, password=self.password,
db=self.dbname, charset='utf8')
except Exception as e:
print(e)
# 上传完毕弹出完成框
tkinter.messagebox.showerror("定制封面应用脚本", "连接数据库失败")
self.cursor = self.db.cursor()