0

I am having the same error in python no matter where I put the db.commit() statement.

Here is my code:

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)

The error that I get is this:

AttributeError("'module' object has no attribute 'commit'",)

And the description that I get is the following:

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 Answer 1

1

You want to call commit on the connection object - which is c in your case. (so make it c.commit() instead of db.commit())

You can find the python dbapi connection methods here.

Sign up to request clarification or add additional context in comments.

2 Comments

that was the answer :) I'm sorry if this was an obvious question, but I'm not so used to code in phyton :)
Don't fret. Every program you write brings you one step closer to mastery. ...and this is one error you likely won't face again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.