[Tutor] MySQLdb : I finally got it to work.

Servando Garcia garcia.servando at gmail.com
Sun Feb 19 02:39:34 CET 2006


Hello list
    It seems that true understanding must , at times, be painful. Danny 
Yoo thank you for not just giving me the answer but rather making me 
work for it. Here is how I got the code to run.

1. With my configuration, I need quotes around the values that are to be 
placed  into the database, otherwise an error is raised example: ( n= 
blue --> Error 1054:n Unknown column 'blue' in 'field list' )
                  n = raw_input("Please enter a Name: ")
                  c =raw_input("Please enter a Color: ")
                  cursor.execute("INSERT INTO horses(name,color) 
VALUES('%s','%s')"%(n,c))

2. I can not simply pass variables in the SQL query. Interpolation is 
necessary. Otherwise an error is raised.

                  n = raw_input("Please enter a Name: ")
                  c =raw_input("Please enter a Color: ")
                  cursor.execute("INSERT INTO horses(name,color) 
VALUES(n,c)" )

            Error 1054:n Unknown column 'n' in 'field list'

3 To update the database I needed an .commit()  ( 
http://www.python.org/peps/pep-0249.html )
 
        I added db.commit() after the INSERT statement and all was well.



Once again thanks to all.


More information about the Tutor mailing list