[Tutor] Get variable values

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Feb 1 19:35:38 CET 2007

>> Wait, wait, before we continue here: did you finally get things working 
>> from your last question?
> Yes, i got things working. That was just a test for python-MySQldb.

Hi Mario,

Ok, good.  Next time, make it more clear to the group that things worked 
out fine, to close the thread.  Remember, we can't read your mind: make it 

Otherwise, people will continue to try to help you with something that you 
don't have problems with anymore.  *grin*

>                     db = MySQLdb.connect(host="localhost", 
> user="planet", passwd="secret", db="planet_geek")
>                     cursor = db.cursor()
>                     cursor.execute("INSERT INTO blog_posts (title)
> VALUES ('entry[key].url')")
> This isn't working.
> It inserts nothing in the database.

There are two things you need to consider:

    1.  "Autocommit" mode is off.  That is, by default, your database 
access is transactioned.  That means that if you don't commit, the 
database rolls any changes right back out as soon as the connection 
closes.  See:


    2.  String literals are really string literals.  That is, the code as 
written above will insert the literal string "entry[key].url" as a title.

Use prepared statements.  See:


for an example.

There's also a third thing I'd suggest: abstract our the database-creation 
into a separate function.  You're probably going to be opening connections 
in several places in your code: keep the connection-open logic in a single 
place so you can more easily change configurations.

If it's possible, pull out the code that touches the database into a 
separate module layer.  People on the list can talk about this more if 
you'd like.

If you have more questions, please free to ask.

More information about the Tutor mailing list