2014-09-24 1 views
0

db.commit() 문을 어디에 두든 파이썬에서 동일한 오류가 발생합니다.Python REST mysql db.commit() 오류

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는

그리고 (" ' 모듈은 ' 목적은 '가 ' "를 저지 어떤 속성을 없음)

설명은 다음과 같습니다.

Traceback (most recent call last): 
File &quot;/usr/local/lib/python2.7/dist-packages/bottle-0.12.7-py2.7.egg/bottle.py&quot;, line 862, in _handle 
return route.call(**args) 
File &quot;/usr/local/lib/python2.7/dist-packages/bottle-0.12.7-py2.7.egg/bottle.py&quot;, line 1729, in wrapper 
rv = callback(*a, **ka) 
File &quot;tarea.preg4.py&quot;, line 33, in registrar 
db.commit() 
AttributeError: &#039;module&#039; object has no attribute &#039;commit&#039; 

답변

1

연결 개체에서 커밋을 호출하려고합니다 (귀하의 경우에는 c). (그러므로 db.commit() 대신 c.commit()으로 지정하십시오.)

here의 python dbapi 연결 방법을 찾을 수 있습니다.

+0

답변 : 나는 이것이 명백한 질문이라면 유감이지만 phyton을 코딩하는 데 익숙하지는 않습니다. – Mgustavoxd1

+0

두려워하지 마십시오. 당신이 쓰는 모든 프로그램은 숙달에 한걸음 더 가까이 다가갑니다. ... 그리고 이것은 당신이 다시는 직면하지 않을 하나의 오류입니다. – Gerrat