AttributeError: 'NoneType' object has no attribute 'commit'
AttributeError: 'NoneType' object has no attribute 'commit'
我正在使用 JetBrains PYCharm 创建一个连接到外部 API 的 Python 脚本来整理天气数据,请原谅我的无知,我知道类似的问题之前有人问过,请多多包涵我这周才第一次接触Python。
它导入'PostcodeToLatLong' - 顾名思义,这是将标准英国邮政编码转换为一对纬度和经度值。
当我尝试 运行 PYCharm 中的代码时,我在 PYCharm 中的调试 window 中得到以下 error/output,我可以看到它与数据库 Connect/Disconnect 功能相关,但不明白为什么我会收到此消息 - 有什么想法吗?:
Exception ignored in: <bound method AA_ForecastIOWeather.__del__ of <__main__.AA_ForecastIOWeather object at 0x01CFB250>>
Traceback (most recent call last):
File "C:/www-service/forecast-io.py", line 18, in __del__
File "C:/www-service/forecast-io.py", line 24, in disconnect_database
AttributeError: 'NoneType' object has no attribute 'commit'
Python 代码如下:-
#!/usr/bin/python
import mysql.connector
import time
import logging
from PostCodeToLatLong import PostCodeToLatLong
API_KEY = 'fa9690c7e2927c7e9696d7xxxxxxxxxxx'
class AA_ForecastIOWeather(object):
def __init__(self, codepoint_dir = '.'):
self._db = None
self.log = self._init_logging()
self._forecastIO = API_KEY
self.p2ll = PostCodeToLatLong(codepoint_dir)
def __del__(self):
self.disconnect_database()
def connect_database():
mysql.connector.connect(user='root', password='admin', host='localhost', port=3306, database='mydbname')
def disconnect_database(self):
self._db.commit()
self._db.close()
def _init_logging(self):
log = logging.getLogger('AA_ForecastIOWeather')
log.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s-%(name)s(%(lineno)d)-%(levelname)s:%(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
log.addHandler(handler)
return log
w = AA_ForecastIOWeather(codepoint_dir = '/registration/lib/codepoint')
设置 self._db
:
变化:
def connect_database():
mysql.connector.connect(user='root', password='admin', host='localhost', port=3306, database='mydbname')
进入:
def connect_database(self):
self._db = mysql.connector.connect(user='root', password='admin', host='localhost', port=3306, database='mydbname')
当然需要调用connect_database()
。如果没有在其他地方完成,这将是一个好地方:
def __init__(self, codepoint_dir = '.'):
self._db = connect_database()
我正在使用 JetBrains PYCharm 创建一个连接到外部 API 的 Python 脚本来整理天气数据,请原谅我的无知,我知道类似的问题之前有人问过,请多多包涵我这周才第一次接触Python。
它导入'PostcodeToLatLong' - 顾名思义,这是将标准英国邮政编码转换为一对纬度和经度值。
当我尝试 运行 PYCharm 中的代码时,我在 PYCharm 中的调试 window 中得到以下 error/output,我可以看到它与数据库 Connect/Disconnect 功能相关,但不明白为什么我会收到此消息 - 有什么想法吗?:
Exception ignored in: <bound method AA_ForecastIOWeather.__del__ of <__main__.AA_ForecastIOWeather object at 0x01CFB250>>
Traceback (most recent call last):
File "C:/www-service/forecast-io.py", line 18, in __del__
File "C:/www-service/forecast-io.py", line 24, in disconnect_database
AttributeError: 'NoneType' object has no attribute 'commit'
Python 代码如下:-
#!/usr/bin/python
import mysql.connector
import time
import logging
from PostCodeToLatLong import PostCodeToLatLong
API_KEY = 'fa9690c7e2927c7e9696d7xxxxxxxxxxx'
class AA_ForecastIOWeather(object):
def __init__(self, codepoint_dir = '.'):
self._db = None
self.log = self._init_logging()
self._forecastIO = API_KEY
self.p2ll = PostCodeToLatLong(codepoint_dir)
def __del__(self):
self.disconnect_database()
def connect_database():
mysql.connector.connect(user='root', password='admin', host='localhost', port=3306, database='mydbname')
def disconnect_database(self):
self._db.commit()
self._db.close()
def _init_logging(self):
log = logging.getLogger('AA_ForecastIOWeather')
log.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s-%(name)s(%(lineno)d)-%(levelname)s:%(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
log.addHandler(handler)
return log
w = AA_ForecastIOWeather(codepoint_dir = '/registration/lib/codepoint')
设置 self._db
:
变化:
def connect_database():
mysql.connector.connect(user='root', password='admin', host='localhost', port=3306, database='mydbname')
进入:
def connect_database(self):
self._db = mysql.connector.connect(user='root', password='admin', host='localhost', port=3306, database='mydbname')
当然需要调用connect_database()
。如果没有在其他地方完成,这将是一个好地方:
def __init__(self, codepoint_dir = '.'):
self._db = connect_database()