split problem if the delimiter is inside the text limiter

Tim Chase python.list at tim.thechases.com
Wed Mar 18 15:26:13 EDT 2009


> sql = ''' INSERT INTO table (column1,column2, ...) VALUES ( %s,
> %s, ....); '''
> for row in rows:
>     connection.cursor.execute(sql % (row[0],row[1],....))
> connection.corsur.commit()
> 
> but something binary in a cell, the pgdb says it is not in utf-8
> format, or something like this.
> I know it's a newbie solution :))
> better solution?

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

As for the UTF-8 exception, without knowing more about the data 
and the table-definitions, it's hard to offer a tangible solution.

-tkc








More information about the Python-list mailing list