无论我在哪里放置db.commit()语句,我都在python中遇到同样的错误。
这是我的代码:
from bottle import route, run
import json
import collections
import MySQLdb as db
@route('/register/<COD>/<NOMBRE>/<APELLIDO>/<DIRECCION>/<TEL>/<COD_FAC>', method='PUT')
def registrar(COD,NOMBRE,APELLIDO,DIRECCION,TEL,COD_FAC):
c=db.connect('10.100.70.136','koala','toor','lab2',use_unicode=True)
cur=c.cursor()
sql1='SELECT * FROM alumnos WHERE codigo="'+COD+'";'
cur.execute(sql1)
alumnos=cur.fetchall();
i=0
for alumno in alumnos:
i+=1
print(i)
if i==0:
operationResult=1
operationMessage=""
cur2=c.cursor()
sql2='INSERT INTO alumnos (codigo,nombre,apellido,direccion,telefono,codigoFacultad) VALUES ("'+COD+'","'+NOMBRE+'","'+APELLIDO+'","'+DIRECCION+'","'+TEL+'","'+COD_FAC+'");'
cur2.execute(sql2)
else:
operationResult=2
operationMessage="El alumno con codigo "+COD+" ya se encuentra registrado"
db.commit()
db.close()
results = []
d=collections.OrderedDict()
d['operationResult'] = operationResult
d['operationMessage'] = operationMessage
results.append(d)
j = json.dumps(results)
return j
run(host='localhost',port=8080,debug=True)
我得到的错误是:
AttributeError(&#34;&#39;模块&#39;对象没有属性&#39;提交&#39;&#34;,)
我得到的描述如下:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/bottle-0.12.7-py2.7.egg/bottle.py", line 862, in _handle
return route.call(**args)
File "/usr/local/lib/python2.7/dist-packages/bottle-0.12.7-py2.7.egg/bottle.py", line 1729, in wrapper
rv = callback(*a, **ka)
File "tarea.preg4.py", line 33, in registrar
db.commit()
AttributeError: 'module' object has no attribute 'commit'
答案 0 :(得分:1)
您想要在连接对象上调用commit - 在您的情况下为c
。 (因此请c.commit()
代替db.commit()
)
您可以找到python dbapi连接方法here。