[Tutor] MySQLdb error - PLEASE SAVE ME!
edhotchkiss at gmail.com
Sat Sep 17 23:11:36 CEST 2005
I got the mysql db just for this very purpose, that's it :P
Ok heres the error which I am getting now, I dont see why your new code
shouldn't work, it makes sense to me ...
Traceback (most recent call last):
File "G:\Python\myCode\Links Database\addfromtext.py", line 30, in ?
File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 129, in
self.errorhandler(self, ProgrammingError, m.args)
File "C:\Python24\Lib\site-packages\MySQLdb\connections.py", line 33, in
raise errorclass, errorvalue
ProgrammingError: not enough arguments for format string
heres the new code ...
# Script to add links from a comma deliminated file to a MySQL database
conn=MySQLdb.connect( host="www.freesql.org <http://www.freesql.org/>",
cursor = conn.cursor()
stmt = "DROP TABLE IF EXISTS links"
stmt = """CREATE TABLE links (
ID INT NOT NULL auto_increment,
primary key (ID)
inp = open ("sites.txt","r")
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
INSERT INTO links (Name, URL, category)
VALUES (%s, %s, %s)""", links
On 9/17/05, Python <python at venix.com> wrote:
> 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
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor