Insert database rows from CSV file

3c273 nospam at nospam.com
Tue Apr 5 10:41:51 EDT 2005


"Steve Holden" <steve at holdenweb.com> wrote in message
news:mailman.1363.1112686436.1799.python-list at python.org...
> 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

Thanks for the heads-up. I think I'm well on my way now.
Louis





More information about the Python-list mailing list