split problem if the delimiter is inside the text limiter

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Mar 18 17:41:01 EDT 2009


Tim Chase a écrit :
>> sql = ''' INSERT INTO table (column1,column2, ...) VALUES ( %s,
>> %s, ....); '''
>> for row in rows:
>>     connection.cursor.execute(sql % (row[0],row[1],....))
>> connection.corsur.commit()
>>

(snip)

> The first step is to use the database's quoting to prevent problems 
> where miscreant characters (such as a single-quote) appear in the data:
> 
>   connection.cursor.exeute(sql, (row[0], row[1]))
> 
> instead of
> 
>   connection.cursor.exeute(sql % (row[0], row[1]))
> 
> (if your columns in your CSV happen to match the order of your INSERT 
> statement, you can just use
> 
>   execute(sql, tuple(row))

Or more simply:

     cursor.execute(sql, row)




More information about the Python-list mailing list