MySQL : Too many connections

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Mar 13 09:54:04 EST 2003


>>>>> "Thomas" == Thomas Weholt <2002 at weholt.org> writes:

    Thomas> I get an exception using MySQL, "Too many connections". I
    Thomas> understand the error and why it occurrs, but how can I
    Thomas> avoid it?? Do I close after each cursor.execute-call or do
    Thomas> I close the connection?? In case of closing after the
    Thomas> execute-call, how ??

    Thomas> What is the proper way of inserting a lot of records and
    Thomas> during this process check rows allready inserted and avoid
    Thomas> the problem above?


I don't know exactly what you mean about checking rows, but here is
how I would insert rows.

#CREATE TABLE test (
#    name VARCHAR(32) NOT NULL,
#    age INTEGER,
#    PRIMARY KEY (name));

import MySQLdb

entries = (('John', 35), ('Miriam', 32), ('Rahel', 5), ('Ava', 1))

db = MySQLdb.connect(db='somedb',
                     host='somehost',
                     user='someuser',
                     passwd='somepass')
c = db.cursor()

c.execute('DELETE FROM test WHERE 1>0;')
for name, age in entries:
    c.execute('INSERT INTO test VALUES ("%s", %d);' % (name, age))

c.execute('SELECT * FROM test;')
results = c.fetchall()
for result in results:
    print result
    


Note, generally it is preferable to use MySQLdb's string
interpolation, but I have been getting weird errors when I try integer
interpolation so I just used python's format string capabilities here.

You said you are getting too many connections.  That suggests that you
are making multiple calls to MySQLdb.connect.  Is that correct?  If
you post some code, it would help up figure out what is going wrong.

John Hunter





More information about the Python-list mailing list