[Tutor] MySQLdb error - PLEASE SAVE ME!

Python python at venix.com
Sat Sep 17 23:02:16 CEST 2005


You should avoid sending the connection info to the list.  Google will
be making this widely available.  Pranksters *will* delete your tables.
Change your password!

Including the error info would help, but chances the following changes
will fix things:

stmt = """CREATE TABLE links (
    ID INT NOT NULL auto_increment,
                    ^^^^^^^^^^^^^^
    Name TEXT,
    URL LONGTEXT,
    Category LONGTEXT,
    primary key (ID)
)"""


for line in inp.readlines():
    #links = map(str, line.split(","))	# values are already strings
    links = line.split(",",2)	# limit to two splits i.e. only use first 2 commas
    arr.append(links)		# arr is not used ???
    cursor.execute ("""
        INSERT INTO links (Name, URL, category)
            VALUES (%s, %s, %s)""", links
        )

You are not supplying an ID value.  I assume that you want MySQL to fill
it in for you.  So you need to make ID an auto_increment field.

The cursor.execute is now getting *two* arguments, the sql and the
values for the insert.  Do not interpolate your values into the SQL
string.  Leave that to the MySQLdb module.  The %s in the VALUES serves
as a placeholder for the module and should not be used by you with the
Python string format (%) operator.  This should work so long as the name
and URL never contain commas.

-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list