[Tutor] Placeholder in string (MySQL instruction)
"Héctor Villafuerte D."
hec.villafuerte at telgua.com.gt
Thu Oct 23 19:24:14 EDT 2003
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
More information about the Tutor
mailing list