[Tutor] MySQLdb: at a complete loss

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Feb 18 20:51:45 CET 2006


> db=MySQLdb.connect(host='localhost',user='root',passwd='081300',db='tut')
>     cursor=db.cursor()
>     name = raw_input("Please enter a Name: ")
>     color =raw_input("Please enter a Color: ")
>     cursor.execute("""INSERT INTO horses(name,color)
> VALUES("%s","%s")"""%(name,color))
         ^^^^^^^^^

Hi Servando,

Don't do this: don't try to interpolate the variables yourself, but pass
the parameters as part of the execution call:

    http://mail.python.org/pipermail/tutor/2003-April/022010.html


> this script runs without error and does not affect the database in any
> manner.  The database does exist. User/Password are valid. I have run
> the above script , unchanged, on both Macintosh and Ubuntu with success.

Are you familiar with database transactions?  Look at the description of
close() in the Python DB API:

    """Close the connection now (rather than whenever __del__ is
       called)...  Note that closing a connection without
       committing the changes first will cause an implicit
       rollback to be performed."""

    (http://www.python.org/peps/pep-0249.html)

Does this last sentence ring some bells?  *grin*

Newest versions of the MySQLdb module do NOT autocommit anymore. You have
to commit at the end or else you're not going to see any mutation affect
the database: every change you make will just roll back.


Good luck!




More information about the Tutor mailing list