Menu

Problems inserting records

Help
mjlivi
2005-03-03
2012-09-19
  • mjlivi

    mjlivi - 2005-03-03

    Hi all,

    This seems like something really trivial, but I cant for the life of me figure it out. Basically Im finding the insertion of records from a python script to a MYSQL database really, really slow. As a test, Ive written the following code:

    conn = MySQLdb.connect(host=host, user=user, passwd=password, db=Test)
    cursor = conn.cursor()

    for i in range(1000):
    cursor.execute(Insert into table01 (dataitem) value ( + str(i) + ))

    conn.close()
    db.close()

    table01 has two columns, an auto-incrementing primary key and dataitem which is an integer.

    Running this code takes around 40 seconds which just seems absurdly slow to me. Any ideas about what might be going wrong here?

    Thanks a lot.

     
    • Andy Dustman

      Andy Dustman - 2005-03-03

      I know you've said you've figured this out, but for posterity, here is the right way to do it (you did more than one dumb thing):

      import MySQLdb
      db = MySQLdb.connect(host=host, user=user, passwd=password, db=Test)
      c = db.cursor()

      make a list of 1-tuples of integers (contrived example)

      data = [ (i,) for i in xrange(1000)]
      c.executemany(""Insert into table01 (dataitem) value (%s)"", data)

      db.commit() # if you are using transactional tables
      db.close()

       
    • Andy Dustman

      Andy Dustman - 2005-03-03

      Oh, and it's VALUES,not VALUE in the SQL, even if there's only one.

       
    • mjlivi

      mjlivi - 2005-03-03

      Forget about it, I'm an idiot. I've fixed it up so that everything is appended in 1 Insert query and things have improved greatly.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.