报错信息:
# 未获取异常如:
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链接数据库的代码变化文章来源:https://www.toymoban.com/news/detail-808562.html
可以发现如下差异,不同点在于pymysql.connect的参数传递存在差异,python370严格限制了必须要传递对应的参数名称,以防止顺序错误。文章来源地址https://www.toymoban.com/news/detail-808562.html
python365
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()
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(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()
到了这里,关于[Python]pymysql对应PyInstaller解析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!