[Tutor] Placeholder in string (MySQL instruction)

"Héctor Villafuerte D." hec.villafuerte at telgua.com.gt
Thu Oct 23 20:08:45 EDT 2003


Lloyd Kvam wrote:

> 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)


Thanks Lloyd!
I got it working like this...

The function call:
    load_data('ldia.proc', 'traf_internat')

The function definition:
    def load_data(proc, table):
        """Loads *.proc files into MySQL @ localhost."""
        tmp = string.replace(os.getcwd()+'\\'+proc, '\\', '\\\\')
        cursor.execute("""LOAD DATA INFILE '%s' INTO TABLE %s FIELDS 
TERMINATED BY ''""" % (tmp,table))





More information about the Tutor mailing list