[Tutor] Placeholder in string (MySQL instruction)

Lloyd Kvam pythontutor at venix.com
Thu Oct 23 17:49:33 EDT 2003


My guess is that table contains
	'traf_internat'
and not simply
	traf_internat

I think it is a good idea to have some mechanism in place to log/print
your sql statements so that you can better debug this kind of problem.
I'd recommend your own execute function which printed or logged the sql
statement before passing it on to cursor execute.

Otherwise, this could be a bug in MySQLdb.  The quick workaround is for
you to do the string interpolation yourself:

cursor.execute("""LOAD DATA INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY ''""" % (proc,table))
(I think I got the quoting correct)

Héctor Villafuerte D. wrote:

> Hi all,
> I'm loading some files into MySQL tables. This function is working fine:
> 
> def load_data(proc):
>    """Loads *.proc files into MySQL @ localhost."""
>    cursor.execute("""LOAD DATA INFILE %s INTO TABLE traf_internat FIELDS 
> TERMINATED BY ''""", (proc,))
> 
> BUT, I would like to pass the table name as an argument also... 
> something like this:
> 
> load_data(os.getcwd()+'\\'+'ldia.proc', 'traf_internat')
> 
> def load_data(proc, table):
>    """Loads *.proc files into MySQL @ localhost."""
>    cursor.execute("""LOAD DATA INFILE %s INTO TABLE %s FIELDS TERMINATED 
> BY ''""", (proc,table))
> 
> Unfortunately, this won't work:
> 
> Traceback (most recent call last):
>  File "c:\Python23\progs\ttp\ttp_build.py", line 88, in ?
>    ttp_mysql.load_data(os.getcwd()+'\\'+'ldia.proc', 'traf_internat')
>  File "c:\Python23\progs\ttp\ttp_mysql.py", line 13, in load_data
>    cursor.execute("""LOAD DATA INFILE %s INTO TABLE %s FIELDS TERMINATED 
> BY ''""", (proc,table))
>  File "C:\Python23\Lib\site-packages\MySQLdb\cursors.py", line 95, in 
> execute
>    return self._execute(query, args)
>  File "C:\Python23\Lib\site-packages\MySQLdb\cursors.py", line 114, in 
> _execute
>    self.errorhandler(self, exc, value)
>  File "C:\Python23\Lib\site-packages\MySQLdb\connections.py", line 33, 
> in defaulterrorhandler
>    raise errorclass, errorvalue
> _mysql_exceptions.ProgrammingError: (1064, "You have an error in your 
> SQL syntax.  Check the manual that corresponds to
> your MySQL server version for the right syntax to use near 
> ''traf_internat' FIELDS TERMINATED BY ''' at line 1")
> 
> Thanks in advance for your help,
> Hector
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list