记一次Python代码错误…
今天心血来潮,一个pymysql感觉不够我玩的(虽然我是菜鸡),想要研究研究Python MySQL连接池(典型的这山望着那山高),但是遇到一个错误 想了好久 百度了好久 最后发现是我对Python理解还是不够深(粗心)导致的.
全部报错:
"D:\package install\Python\python.exe" E:/桌面快捷方式/pycharm/python/geren/mysql数据库操作/MySQL连接池.py
Traceback (most recent call last):
File "D:\package install\Python\lib\site-packages\DBUtils\PooledDB.py", line 325, in connection
con = self._idle_cache.pop(0)
IndexError: pop from empty list
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/桌面快捷方式/pycharm/python/geren/mysql数据库操作/MySQL连接池.py", line 5, in <module>
pool = PooledDB(pymysql,20,host='10.0.0.4',database='excel_data',port='3306',user='root',password='Sinobase@123')
File "D:\package install\Python\lib\site-packages\DBUtils\PooledDB.py", line 267, in __init__
idle = [self.dedicated_connection() for i in range(mincached)]
File "D:\package install\Python\lib\site-packages\DBUtils\PooledDB.py", line 267, in <listcomp>
idle = [self.dedicated_connection() for i in range(mincached)]
File "D:\package install\Python\lib\site-packages\DBUtils\PooledDB.py", line 338, in dedicated_connection
return self.connection(False)
File "D:\package install\Python\lib\site-packages\DBUtils\PooledDB.py", line 327, in connection
con = self.steady_connection()
File "D:\package install\Python\lib\site-packages\DBUtils\PooledDB.py", line 273, in steady_connection
return connect(
File "D:\package install\Python\lib\site-packages\DBUtils\SteadyDB.py", line 137, in connect
return SteadyDBConnection(
File "D:\package install\Python\lib\site-packages\DBUtils\SteadyDB.py", line 192, in __init__
self._store(self._create())
File "D:\package install\Python\lib\site-packages\DBUtils\SteadyDB.py", line 211, in _create
con = self._creator(*self._args, **self._kwargs)
File "D:\package install\Python\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "D:\package install\Python\lib\site-packages\pymysql\connections.py", line 325, in __init__
self.connect()
File "D:\package install\Python\lib\site-packages\pymysql\connections.py", line 589, in connect
self.host_info = "socket %s:%d" % (self.host, self.port)
TypeError: %d format: a number is required, not str
进程已结束,退出代码 1
import pymysql
# from g_conf.config import config_template
from DBUtils.PooledDB import PooledDB
pool = PooledDB(pymysql,20,host='10.0.0.4',database='excel_data',port='3306',user='root',password='Sinobase@123')
conn = pool.connection()
cur = conn.cursor()
sql = 'select * from user;'
cur.execute(sql)
#r = cur.commit()
re = cur.fetchall()
print(re)
cur.close()
conn.close()
简短精悍的小代码,错误原因:
一开始下意识的就百度,结果大半天啥进展没有
后来群里问大佬,一起吹了半小时牛逼
后来仔细捋了捋,看了看报错,它提示我 “(self.host, self.port)”,然后仔细看了看相关代码,如下:
pool = PooledDB(pymysql,20,host='10.0.0.4',database='excel_data',port='3306',user='root',password='Sinobase@123')
我的端口 port=“3306” 加了引号,Python将其识别为字符串类型,而这里需要的是数字类型.
写代码是个细心活啊…