[Tutor] MySQLdb: at a complete loss

Brian Gustin brian at daviesinc.com
Sat Feb 18 18:47:40 CET 2006



Servando Garcia wrote:
> Hello List
>     I am at a complete loss. Here is a recap, just to be clear, I have 
> been unable insert into a database using MySQLdb. I can only view the 
> database.
> My current configuration:
>     1. Windows XP
>     2. Python 2.4.2
>     3 MySQLdb current version
>     4. mysql  5.0.18-nt
>     5. DEV-c++ current version.
> 
> My last script was very sloppy here is my current version
> #!/usr/bin/python
> import MySQLdb,pdb
> 
> #pdb.set_trace()
> try:
>     
> 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))
>     cursor.close()
I might suggest single quoting the values as in
return = cursor.execute("""INSERT INTO horses(name,color)
VALUES('%s','%s')"""%(name,color))
print return # get a return value of the operation and print it - helps 
in debuugging

>    
> except MySQLdb.OperationalError,message:
>     errorMessage ='Error %d:n %s'%(message[0],message[1])
>     print errorMessage
> 
> 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.
> 
I notice that some of your activities have no return value - these are 
highly useful in debugging- typically I will capture return values into 
a list or dict so I can examine them after the script executes, or empty 
them out after wards if I set debugging = off
in my scripts.

Bri!

> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:43f74a3f294671847412610!
> 
> 


More information about the Tutor mailing list