AttributeError: 'NoneType' object has no attribute 'execute' 错误的解决
2017-07-15 11:31
1956 查看
运行Python连接mysql数据,查询表格中的数据时报了如下错误AttributeError: 'NoneType' object has no attribute 'execute' 的解决方案
python get_uv_pv_compute_results_from_database.py
Traceback (most recent call last):
File "get_uv_pv_compute_results_from_database.py", line 116, in <module>
obi_get_pv_uv_result_from_database.get_day_pv_uv_from_mysql()
File "get_uv_pv_compute_results_from_database.py", line 73, in get_day_pv_uv_from_mysql
res_num = self.execute_pv_uv(sql_get_day_pv_uv_results)
File "get_uv_pv_compute_results_from_database.py", line 40, in execute_pv_uv
return self.conn_pv_uv.execute(sql)
File "/home/yaoyingzhe/2222/db.py", line 33, in execute
return self.cursor.execute(sql)
AttributeError: 'NoneType' object has no attribute 'execute'
解决方案:
python get_uv_pv_compute_results_from_database.py
Traceback (most recent call last):
File "get_uv_pv_compute_results_from_database.py", line 116, in <module>
obi_get_pv_uv_result_from_database.get_day_pv_uv_from_mysql()
File "get_uv_pv_compute_results_from_database.py", line 73, in get_day_pv_uv_from_mysql
res_num = self.execute_pv_uv(sql_get_day_pv_uv_results)
File "get_uv_pv_compute_results_from_database.py", line 40, in execute_pv_uv
return self.conn_pv_uv.execute(sql)
File "/home/yaoyingzhe/2222/db.py", line 33, in execute
return self.cursor.execute(sql)
AttributeError: 'NoneType' object has no attribute 'execute'
解决方案:
1.mysql数据库没有正确连接,你确认所以参数都对吗?没有漏掉哪个?比如端口。 def Connect(self, *args, **kwargs): self.conn = MySQLdb.connect(*args, **kwargs) self._cursor = self.conn.cursor()
2. 在使用mysql的mysql语句进行查询等操作之前,应该先连接mysql数据库,然后进行查询等操作,最后在完成操作之后,一定要关闭连接
def connect(self): self.connection = MySQLdb.connect(host=self.my_host, user=self.my_user, passwd=self.my_passwd, db=self.my_db, port=int(self.my_port), charset=self.my_charset) self.cursor = self.connection.cursor() def close(self): self.cursor.close() self.connection.close() def execute(self, sql): return self.cursor.execute(sql) def fetchall(self): return self.cursor.fetchall() def commit(self): #self.cursor.close() self.connection.commit()
相关文章推荐
- AttributeError: 'NoneType' object has no attribute 'copy' 解决办法
- 使用Buildozer部署时,出现AttributeError: 'NoneType' object has no attribute 'group'错误的解决
- AttributeError: 'NoneType' object has no attribute 'sc' 解决方法(二)
- AttributeError: 'module' object has no attribute '_handlerList' 错误解决方法
- Keras深度学习框架学习笔记(3) - AttributeError:’module’ object has no attribute ‘ifelse’错误信息的解决方法
- Ubuntu运行scrapy报“scrapy: 'module' object has no attribute 'OP_SINGLE_ECDH_USE'错误解决方法”
- AttributeError: 'module' object has no attribute 的解决方法
- Python3.4 pandas 在IDLE出现AttributeError: 'NoneType' object has no attribute 'fileno'
- 解决方法——AttributeError: 'module' object has no attribute 'VideoWriter_fourcc'
- AttributeError: ‘NoneType’ object has no attribute ‘clone’
- AttributeError: 'module' object has no attribute '_base' 可行解决办法
- 'NoneType' object has no attribute '__getitem__'和argument of type 'NoneType' is not iterable 异常的解决办法
- AttributeError: 'module' object has no attribute 'SSL_ST_INIT' 解决办法
- AttributeError: 'module' object has no attribute 的解决方法
- AttributeError: module 'requests' has no attribute 'get'”,python中”requests模块“get”属性缺失“的错误该如何解决?
- GNU Radio自定义模块在添加到流图后运行报‘module’ object has no attribute错误的解决方法
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- AttributeError: 'NoneType' object has no attribute 'astyp
- yum错误'module' object has no attribute 'iterparse'解决方法
- 解决问题 AttributeError: 'module' object has no attribute 'face_recognition_model_v1'