Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:12:08+00:00 2026-06-03T02:12:08+00:00

I’m loading a csv file into my database via a web form. The order

  • 0

I’m loading a csv file into my database via a web form.

The order of the raw data is consistent in each csv file, but it changes from file to file, depending on the source, so I have a preview form that shows five rows and allows you to assign a column via a drop-down list of valid column names in the table.

Then I use the cgi form to build an INSERT statement, and parse the csv file line-by-line to populate the table.

But it is running EXTREMELY slow. I’m concurrently populating two tables, one with 961402 rows (7 columns with values), and the other with 1835538 rows(1 column with values), and each has been running for at least half an hour. I’m only seeing something like 100 new rows per second.

Can you see anything here that would slow me down?

NOTE: I know there is some ugly code in here, it was one of the first python cgi scripts I wrote while figuring this language out.

 for item in form:
          field = form.getvalue(item)
          field = cgi.escape(field)
          if field == 'null':
                  pass
          elif item == 'csvfile':
                  pass
          elif item == 'campaign':
                  pass
          elif item == 'numfields':
                  pass
          else:
                  colname = str(colname) + ", " + str(item)

                  colnum.append(field)
  assert(numfields > 0)
  placeholders = (numfields-1) * "%s, " + "%s"
  query = ("insert into %s (%s)" % (table, colname.lstrip(",")))
  with open(fname, 'rb') as f:
          reader = csv.reader(f)
          try:
                  record = 0
                  errors = 0
                  for row in reader:
                          try:
                                  record = record + 1
                                  data = ''
                                  for value in colnum:
                                          col = int(value)
                                          rawrow = row[col]
                                          saferow = rawrow.replace("'", "-")
                                          saferow = saferow.replace("-", "")
                                          data = str(data) + ", '" + saferow + "'"
                                  dataset = data.lstrip(',')
                                  insert = query + (" values (%s)" % dataset)
                                  cur.execute(insert)
                                  con.commit()
                                  print ".",
                          except IndexError, e:
                                  print "Row:%d file %s, %s<br>" % (reader.line_num, fname.lstrip("./files/"), e)
                                  errors = errors + 1
                          except csv.Error, e:
                                  print "Row:%s file %s, line %d: %s<br>" % (record, fname, reader.line_num, e)
                                  errors = errors + 1
                          except mdb.Error, e:
                                  print "Row:%s Error %d: %s<br>" % (record, e.args[0], e.args[1])
                                  errors = errors + 1
                          except:
                                  t,v,tb = sys.exc_info()
                                  print "Row:%s %s<br>" % (record, v)
                                  errors = errors + 1
          except csv.Error, e:
                  print "except executed<br>"
                  sys.exit('file %s, line %d: %s' % (fname, reader.line_num, e))
  print "Succesfully loaded %s into Campaign %s, <br>" % (fname.lstrip("./files/"), table)
  print record - errors, "new records.<br>"
  print errors, "errors.<br>"

EDIT/UPDATE: Using LOAD DATA LOCAL INFILE worked like a charm, I loaded up 600K records in less than a minute.

New Code is cleaner, too.

    else:
            colnum.append([field, item])
sortlist =  sorted(colnum, key=itemgetter(0))
cols = ''
for colname in sortlist:
    cols = cols + "%s, " % colname[1]
cur.execute("LOAD DATA LOCAL INFILE '%s' IGNORE INTO TABLE %s FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (%s)" % (fname, table, cols.rstrip(', ')))
con.commit()

The only catch is that I have to do a smidge more work preparing my csv files to ensure data integrity, otherwise, works like a charm.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:12:09+00:00Added an answer on June 3, 2026 at 2:12 am

    INSERT INTO, done one row at a time, is pretty slow considering that some SQLs, like mySQL, support either having a bunch of rows on a single insert command or LOAD DATA statements that can read CSV files quickly into the server.

    See also: https://dba.stackexchange.com/questions/16809/why-is-load-data-infile-faster-than-normal-insert-statements

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I have a reasonable size flat file database of text documents mostly saved in
I used javascript for loading a picture on my website depending on which small
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.