python 错误:"'NoneType' object has no attribute 'execute'" 求解 谢谢!
importMySQLdbclassmysql():def__enter(self):#InanyMultiTaskingenvironmenttheabilitytoa...
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 <mod
ule>
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 运行结果:
<_mysql.connection open to 'localhost' at bb5608>
None
问题就在self._cursor = self.conn.cursor()这里 求分析 求解释..
谢谢! 展开
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 <mod
ule>
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 运行结果:
<_mysql.connection open to 'localhost' at bb5608>
None
问题就在self._cursor = self.conn.cursor()这里 求分析 求解释..
谢谢! 展开
展开全部
感觉数据库没有正确连接,你确认所以参数都对吗?没有漏掉哪个?比如端口。
def Connect(self, *args, **kwargs):
self.conn = MySQLdb.connect(*args, **kwargs)
self._cursor = self.conn.cursor()
在这里先打印这两个对象看一下,看有没有正确创建。
def Connect(self, *args, **kwargs):
self.conn = MySQLdb.connect(*args, **kwargs)
self._cursor = self.conn.cursor()
在这里先打印这两个对象看一下,看有没有正确创建。
更多追问追答
追问
端口默认3306啊 我照你的方法在两句后面各打印了print self.conn和print self._cursor 运行结果和上面的一样 说明这两个对象没问题呀 为什么一传到begin()里面就这个鸟样了
追答
我是让你打印的那两个对象的地址是什么,类型的什么,能贴出来看看吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询