473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Insert database rows from CSV file

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
Jul 18 '05 #1
6 2885
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/...p_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

Jul 18 '05 #2
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
could just insert them into his table and save him some steps. I'm also just
trying to learn the basics Python and SQL . Thanks again.
Louis

"Larry Bates" <lb****@syscononline.com> wrote in message
news:Cu********************@comcast.com...
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/...p_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

Jul 18 '05 #3
On Mon, 4 Apr 2005 15:54:37 -0700, "3c273" <no****@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))
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4
Dennis Lee Bieber wrote:
On Mon, 4 Apr 2005 15:54:37 -0700, "3c273" <no****@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/

Jul 18 '05 #5

"Dennis Lee Bieber" <wl*****@ix.netcom.com> wrote in message
news:bp********************************@4ax.com...
On Mon, 4 Apr 2005 15:54:37 -0700, "3c273" <no****@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))


Ah.. Many thanks. This is what I was looking for.
Louis
Jul 18 '05 #6

"Steve Holden" <st***@holdenweb.com> wrote in message
news:ma**************************************@pyth on.org...
Dennis Lee Bieber wrote:
On Mon, 4 Apr 2005 15:54:37 -0700, "3c273" <no****@nospam.com> declaimed
the following in comp.lang.python:

Thanks for the link, but this is the step I am trying to save (for someoneelse). Every time he goes to run a report, he must stop and import any newcsv 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
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: RotterdamStudents | last post by:
Hello there, i have a strange problem. I can't get php to insert multiple rows at once in a MySQL database. I use the $sql = "INSERT INTO database (a,b,c,d,e) VALUES ('$a', '$b' ,'$c', '$d',...
7
by: iqbal | last post by:
Hi all, We have an application through which we are bulk inserting rows into a view. The definition of the view is such that it selects columns from a table on a remote server. I have added the...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
9
by: Curtis Stanford | last post by:
I'm in a situation where I need to load an ASCII file into a database. No sweat so far right? The records are indexed by date and I have a unique index on date. The ASCII can overlap, meaning it...
1
by: Pesko S | last post by:
Hi, Could anybody just point me in a direction where I can find information on how the heck I can update a database with relational data from an XML file. I use stored procedures to insert...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
11
by: Sezai YILMAZ | last post by:
Hello I need high throughput while inserting into PostgreSQL. Because of that I did some PostgreSQL insert performance tests. ------------------------------------------------------------ --...
46
by: dunleav1 | last post by:
I have a process that does inserts that runs 50% slower that Oracle and Mssql. Queries normally take 50% slower than normal. DB2, Oracle, Mssql all are configured on same os, same disk array...
0
by: Analizer1 | last post by:
Hello, c#2 , sqlserver 2005 I am parsing a Text File into 2 Datatables Parent we'll say invoice child well say Invoice Items parent rows 10,000, child rows 30,000 the 2 Tables start out...
2
by: cday119 | last post by:
Can someone help me out with this one. I am trying to insert a row of data into an access database. The primary key is set to auto increment. It will work once but the next time you try an insert...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.