Insert database rows from CSV file

Steve Holden steve at holdenweb.com
Tue Apr 5 03:32:47 EDT 2005


Dennis Lee Bieber wrote:
> On Mon, 4 Apr 2005 15:54:37 -0700, "3c273" <nospam at nospam.com> declaimed
> the following in comp.lang.python:
> 
> 
>>Thanks for the link, but this is the step I am trying to save (for someone
>>else). Every time he goes to run a report, he must stop and import any new
>>csv files. Since the files are generated by a Python script, I thought I
> 
> 
> 	That information wasn't supplied in the original message. Your
> original post implied that the data source /was/ the CSV file...
> 
> 	Show us the code segment that is writing the CSV file, and we
> can probably show you the DB-API equivalent for "writing" a new record
> to the table.
> 
> For short however:
> 
> aCustomer = "Customer1"
> theWidget = "Widget1"
> aQuantity = 1000
> 
> # I'm presuming the table only has the three columns, since you didn't
> list fields
> cursor.execute(""" INSERT INTO "Table1" Values (%s, %s, %s) """,
> 		(aCustomer, theWidget, aQuantity))

Beware, however, that the parameter markers ("%s" in the example above) 
will depend on which database module you use - some modules will expect 
"?", for example. This depends on the module's "paramstyle".

Also, don't forget to commit the changes!

regards
  Steve
-- 
Steve Holden        +1 703 861 4237  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/




More information about the Python-list mailing list