Insert database rows from CSV file

Larry Bates lbates at syscononline.com
Mon Apr 4 18:16:58 EDT 2005


You may want to take a look at this link.  It should
be much faster than any programmatic technique.

http://www.its.niu.edu/its/CSupport/tipoftheweek/tip_080502.shtml

If you still want to do it programmatically, you will need to
look at csv module to parse the lines.

Larry Bates



3c273 wrote:
> Hello,
> I have a really simple Access database table with a format similar to this:
> CustomerName - ProductOrdered - QtyOrdered
> 
> I have a CSV file with the appropriate values as follows:
> Customer1, Widget1, 1000
> Customer2, Widget2, 3000
> etc
> 
> I have figured out how to insert the data manually from the interactive
> prompt:
> cursor.execute(""" INSERT INTO "Table1" Values ('Customer1', "Widget1',
> '1000') """)
> 
> What I would like to do is iterate over the CSV file like this:
> for lines in file:
>     cursor.execute(""" INSERT INTO "Table1" lines """)
> 
> I have googled and found some examples that use string formatting, but
> without documentation I can't seem to find the right formula. I don't have
> any problem with the iteration part, I just can't seem to figure out how to
> use a variable to insert an entire row with the INSERT statement. Can anyone
> get me going in the right direction? I'm using odbc from win32all,
> Python2.3, and Access2000 if it matters. Thanks.
> Louis
> 
> 



More information about the Python-list mailing list