早教吧作业答案频道 -->其他-->
python错误:"'NoneType'objecthasnoattribute'execute'"importMySQLdbclassmysql():defenter(self):#InanyMultiTaskingenvironmenttheabilitytoatomicallyexecuteasectionofcodeisveryimportant.Tocreateacriticalsectioninstackles
题目详情
python 错误:"'NoneType' object has no attribute 'execute'"
import MySQLdb
class mysql():
def __enter(self):#In any MultiTasking environment the ability to atomically execute a section of code is very important.To create a critical section in stackless
self.__tasklet = stackless.getcurrent()
self.__atomic = self.__tasklet.set_atomic(True)
def Connect(self,*args,**kwargs):
self.conn = MySQLdb.connect(*args,**kwargs)
self._cursor = self.conn.cursor()
self._trans = False
#After mysql-python 1.12,* Turn autocommit off initially
#But here we enable autocommit again.
self.conn.autocommit(True)
self._args = args
self._kwargs = kwargs
return self.conn
#MySQL python Version 1.1.2
# * Remove Connection.begin(); use SQL BEGIN or START TRANSACTION instead self.conn.begin()
def Begin(self):
#If already start transaction,donn't restart it.maybe commit the last trans
if self._trans:return
self._cursor.execute("BEGIN")
self._trans = True
def Insert(self,sql,para ):
self.__enter()
_cursor = self.conn.cursor()
_cursor.execute(sql,para)
ret = _cursor.lastrowid
_cursor.close()
self.__exit()
return ret
f=mysql()
f.Connect(user="root",passwd="12345",host="localhost",db="yingtest")
f.Begin()
f.Insert("insert into note values('%s,%s')"%[4,'yes'])
运行错误:
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\桌面\12345.py",line 38,in
f.Begin()
File "C:\Documents and Settings\Administrator\桌面\12345.py",line 23,in Begi
n
self._cursor.execute("BEGIN")
AttributeError:'NoneType' object has no attribute 'execute'
按照分析来讲 我想是f.Connect(user="root",passwd="12345",host="localhost",db="yingtest")这句没运行成功 求分析 .
在 self.conn = MySQLdb.connect(*args,**kwargs)
和 self._cursor = self.conn.cursor()
后面各打印了print self.conn和print self._cursor 运行结果:
None
问题就在self._cursor = self.conn.cursor()这里 求分析 .
import MySQLdb
class mysql():
def __enter(self):#In any MultiTasking environment the ability to atomically execute a section of code is very important.To create a critical section in stackless
self.__tasklet = stackless.getcurrent()
self.__atomic = self.__tasklet.set_atomic(True)
def Connect(self,*args,**kwargs):
self.conn = MySQLdb.connect(*args,**kwargs)
self._cursor = self.conn.cursor()
self._trans = False
#After mysql-python 1.12,* Turn autocommit off initially
#But here we enable autocommit again.
self.conn.autocommit(True)
self._args = args
self._kwargs = kwargs
return self.conn
#MySQL python Version 1.1.2
# * Remove Connection.begin(); use SQL BEGIN or START TRANSACTION instead self.conn.begin()
def Begin(self):
#If already start transaction,donn't restart it.maybe commit the last trans
if self._trans:return
self._cursor.execute("BEGIN")
self._trans = True
def Insert(self,sql,para ):
self.__enter()
_cursor = self.conn.cursor()
_cursor.execute(sql,para)
ret = _cursor.lastrowid
_cursor.close()
self.__exit()
return ret
f=mysql()
f.Connect(user="root",passwd="12345",host="localhost",db="yingtest")
f.Begin()
f.Insert("insert into note values('%s,%s')"%[4,'yes'])
运行错误:
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\桌面\12345.py",line 38,in
f.Begin()
File "C:\Documents and Settings\Administrator\桌面\12345.py",line 23,in Begi
n
self._cursor.execute("BEGIN")
AttributeError:'NoneType' object has no attribute 'execute'
按照分析来讲 我想是f.Connect(user="root",passwd="12345",host="localhost",db="yingtest")这句没运行成功 求分析 .
在 self.conn = MySQLdb.connect(*args,**kwargs)
和 self._cursor = self.conn.cursor()
后面各打印了print self.conn和print self._cursor 运行结果:
None
问题就在self._cursor = self.conn.cursor()这里 求分析 .
▼优质解答
答案和解析
感觉数据库没有正确连接,你确认所以参数都对吗?没有漏掉哪个?比如端口.def Connect(self,*args,**kwargs):self.conn = MySQLdb.connect(*args,**kwargs)self._cursor = self.conn.cursor()在这里先打印这两个对象看...
看了 python错误:"'Non...的网友还看了以下:
针对这条语句查表时很慢,怎么才能快一些$orderdetail['yk']=$db->execute 2020-03-31 …
implement和execute都有v.执行的意思/那这两个词有什么区别呢?在句中的用法? 2020-04-13 …
MicrosoftJETDatabaseEngine错误'80040e14'INSERTINTO语 2020-04-27 …
英语翻译翻译图纸过程中,遇到execute2times1xASDRAWN2xMIRRORIMAGE 2020-05-14 …
英语翻译The virtual manufacturing approach in this pa 2020-05-15 …
matlab中使用polyfit函数报错试用了几个例子,执行polyfit函数时无一例外报错:At 2020-05-16 …
这个matlab的M文件为何无法绘制?close all clc clear t=[0:pi/25 2020-05-16 …
使用matlab R2009a时调用函数 imhist 时报错!我在调用函数时,若直接在命令窗口使 2020-05-17 …
python错误:"'NoneType'objecthasnoattribute'execute' 2020-05-19 …
Shell.Execute(SessionVar.Expand("%AppFolder%\\WGS 2020-06-15 …