MySQLdb error - PLEASE SAVE ME!

Carsten Haese carsten at uniqsys.com
Sun Sep 18 06:32:50 CEST 2005


On Sat, 17 Sep 2005 15:50:29 -0400, Ed Hotchkiss wrote
> Ok. I am trying to read a csv file with three strings separated by commas.
> I am trying to insert them into a MySQL DB online.
> MySQLdb is installed, no problems.
> 
> I think that I am having some kind of error with my csv going into 
> the fields and being broken apart correctly. Can someone please 
> help? I attached the code below, it does work with that SQL server 
> also if you want to try and run it. Thanks in advance ..

There are two obvious faults in your code:

1) You're using the % operator to plug your parameters directly into the query
string. Don't do that. Pass the parametrized query as the first argument to
execute(), pass the parameter tuple as the second argument to execute(), and
leave it to execute() to plug in the parameters.

Once 1) is fixed, you'll run into...

2) The ID column is a NOT NULL column. There is no default value for it, nor
is there an AUTO_INCREMENT flag on it. You're not specifying a value for ID in
the insert statement. Either this will fail on the first insert (attempting to
insert a NULL value into a NOT NULL column), or in case MySQL helpfully
defaults the ID to 0, it'll fail on the second row when the primary key
constraint is violated by attempting to insert another 0.

To fix 2), you'll wither want to make the ID column an AUTO_INCREMENT column
or explicitly specify a value for the ID column in the insert statement.
(Using AUTO_INCREMENT is easier.)


Hope this helps,

Carsten.


P.S. You need to learn how to describe your problem accurately. Phrases like
"no problems" and "it does work" juxtaposed with "some kind of error" do
nothing to describe what actually works and what doesn't work. I'm betting
python raised an exception when you ran your code. Instead of guessing
randomly (and incorrectly) that there is some kind of error in your csv
parsing, you could have, for example, included a copy of the exception message.



More information about the Python-list mailing list