2

I am trying to connect to and insert into a MySQL database on my machine following this tutorial and I am stumped. Everything seems to look good up until the point where the the change is actually committed. I am getting an error:

Traceback (most recent call last):
  File "write.py", line 18, in <module>
    db.commit()
AttributeError: 'module' object has no attribute 'commit'

I have followed these instructions to install Python on my Mac and my code looks like this:

#!usr/bin/python

import MySQLdb as db

try:
    con = db.connect(host='localhost', user='trevor', passwd='pw', db='aqi');

    cur = con.cursor()

    sql_statement = """INSERT INTO aqi(datetime, no2, o3, so2, co, pm10, pm25, aqi, health_range)
                       VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
    print sql_statement

    cur.execute(sql_statement, ("some date", 13.2, 53.5, 45.4, 31.1, 31.1, 32.3, 33.4, "healthy"))

    db.commit()

except db.Error, e:

    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)

finally:

  if con:
    con.close()

I haven't found any reference to this type of error anywhere and it really doesn't make sense. I would really appreciate any help or direction. Thanks!

2
  • 1
    note datetime is reserve work don't use it as column name, else use ` Commented Feb 21, 2015 at 7:09
  • thank you @GrijeshChauhan. I appreciate the note. I am still pretty new to SQL. Commented Feb 21, 2015 at 7:20

1 Answer 1

1

You need do connection commit instead of db:

con.commit()

db is alias for module MySQLdb, con is connection object.

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

Comments

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.