这个init 中为什么不能使用 try 呢?报错了,怎么修改一下
大神们,这个init 中为什么不能使用 try 呢?报错了,怎么修改一下import pymysql
import time
class DBconnect(object):
connects = {
'host': '192.168.1.1',
'port': 3306,
'user': 'root',
'passwd': '12345',
'db': 'test',
}
def __init__(self, connects):
try:
# 连接数据库
connect = pymysql.Connect(**connects)
# 获取游标
self.cursor = connect.cursor()
except:
print('数据库连接失败,3s后重试')
time.sleep(3)
报错内容? 报什么错 connects = {
'host': '192.168.1.1',
'port': '3306',
'user': 'root',
'passwd': '12345',
'db': 'test',
}
报错应该是因为你 'port' 的 3306 忘记带引号了吧 我猜房子风水不好,把房子烧了换一栋 Twilight6 发表于 2020-5-23 12:58
报错应该是因为你 'port' 的 3306 忘记带引号了吧
不是跟这个没关系 pokeyboa 发表于 2020-5-23 13:08
不是跟这个没关系
那就请发报错信息呗 我才你人不好,死了再投一胎
>>> connects = {
... 'host': '192.168.100.132',
... 'port': 3306,
... 'user': 'root',
... 'passwd': '12312425345',
... 'db': 'aaa',
... 'charset': 'utf8'
... }
>>> c=db(connects)
随便写个连接,肯定是不通的,下面是报错
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/mystic/Projects/functions/dbconnect.py", line 18, in __init__
try:
File "/home/mystic/.virtualenvs/bdpy/lib/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/home/mystic/.virtualenvs/bdpy/lib/python3.6/site-packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "/home/mystic/.virtualenvs/bdpy/lib/python3.6/site-packages/pymysql/connections.py", line 599, in connect
self._request_authentication()
File "/home/mystic/.virtualenvs/bdpy/lib/python3.6/site-packages/pymysql/connections.py", line 861, in _request_authentication
auth_packet = self._read_packet()
File "/home/mystic/.virtualenvs/bdpy/lib/python3.6/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/home/mystic/.virtualenvs/bdpy/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/home/mystic/.virtualenvs/bdpy/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1045, "Access denied for user 'root'@'192.168.100.131' (using password: YES)")
init 不能返回结果吧? pokeyboa 发表于 2020-5-23 13:24
init 不能返回结果吧?
对的 __new__ 可以
页:
[1]