INSERT statements not INSERTING when using mysql from python

Ben Benjamin.Barker at gmail.com
Fri Dec 29 06:37:56 EST 2006


Ben wrote:
> Well that's odd...
>
> If I place the exact same Insert statement elswhere in the program it
> works as intended.
> That would suggest it is never being run in its old position, but the
> statements either side of it are printing...
>
>
> Ben wrote:
> > I don't know whether anyone can help, but I have an odd problem. I have
> > a PSP (Spyce) script that makes many calls to populate a database. They
> > all work without any problem except for one statement.
> >
> > I first connect to the database...
> >
> > self.con = MySQLdb.connect(user=username, passwd =password)
> > self.cursor = self.con.cursor()
> > self.cursor.execute("SET max_error_count=0")
> >
> > All the neccesary tables are created...
> >
> > self.cursor.execute("CREATE DATABASE IF NOT EXISTS "+name)
> > self.cursor.execute("USE "+name)
> >
> > self.cursor.execute("CREATE TABLE IF NOT EXISTS networks (SM
> > varchar(20),DMC int,DM varchar(50),NOS int,OS varchar(50),NID
> > varchar(20))
> >
> > Then I execute many insert statements in various different loops on
> > various tables, all of which are fine, and result in multiple table
> > entries. The following one is executed many times also. and seems
> > identical to the rest. The print statements output to the browser
> > window, and appear repeatedly, so the query must be being called
> > repeatedly also:
> >
> > print "<p><b>SQL query executing</b><p>"
> > self.cursor.execute("INSERT INTO networks VALUES ('a',' "+i+"
> > ','c','2','e','f','g')")
> > print "<p><b>SQL query executed</b><p>"
> >
> > I have, for debugging, set "i" up as a counter variable.
> >
> > No errors are given, but the only entry to appear in the final database
> > is that from the final execution of the INSERT statement (the last
> > value of i)
> >
> > I suspect that this is to vague for anyone to be able to help, but if
> > anyone has any ideas I'd be really grateful :-)
> >
> > It occured to me that if I could access the mysql query log that might
> > help, but I was unsure how to enable logging for MysQL with python.
> >
> > Cheers,
> >
> > Ben

Well, it would appear to be some kind of autocommit problem. I had
autocommit off, bus committed when I disconnected from the database.
For some reason although all the other statements seemed ok, multiple
statements in that loop were'nt every commiting. For the moment I've
turned autocommit on and that has sorted things out, but it slows
things down too so I'll try to fix it :-)

Ben




More information about the Python-list mailing list