[Tutor] unable to insert data into database

Wolfram Kraus kraus at hagen-partner.de
Fri Feb 17 16:15:17 CET 2006


Wolfram Kraus wrote:
> Servando Garcia wrote:
> 
>>Hello to all
>>    I have been trying to insert data into a database using MySQLdb  
>>with no success. I have no idea why this script is not working. I took 
>>it from a tutorial from DEV Shed.
>>
>>#!/usr/bin/python
>>import MySQLdb
>># connect
>>db = MySQLdb.connect(host="localhost", user="root", 
>>passwd="******",db="tutorial")
>>
>># create a cursor
>>cursor = db.cursor()
>>
>>#User Input
>>name = raw_input("Please enter a name: ")
>>color = raw_input("Please enter a color: ")
>>
>>#Insert data into table "Horses"
>>cursor.execute("INSERT INTO horses(Names,Color) VALUE (%s,%s)",(name,color))
>>
>>
> 
> First of all: Does the database "tutorial" exists? Is there a table 
> "horses" in that database?
> Please show the complete error message you get. The code looks good so far.
> 
> HTH
> Wolfram
> 

Servando Garcia wrote:
##
I have tested the database from a MySQL command line and yes the 
database exists so does the table. The code runs without error but the 
database is not affected
##

Please keep the replies on the list!

Sorry for asking the obvious (Database/Table exists)! You are missing

db.commit()
db.close()

in your code. IIRC auto-commit was switched of for MySQLdb long time ago.

HTH,
Wolfram



More information about the Tutor mailing list