python 错误:"'NoneType' object has no attribute 'execute'" import MySQLdbclass 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 stackles

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/15 01:04:16
python 错误:

python 错误:"'NoneType' object has no attribute 'execute'" import MySQLdbclass 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 stackles
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()这里 求分析 .

python 错误:"'NoneType' object has no attribute 'execute'" import MySQLdbclass 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 stackles
感觉数据库没有正确连接,你确认所以参数都对吗?没有漏掉哪个?比如端口.
def Connect(self,*args,**kwargs):
self.conn = MySQLdb.connect(*args,**kwargs)
self._cursor = self.conn.cursor()
在这里先打印这两个对象看一下,看有没有正确创建.