MySql

John Machin sjmachin at lexicon.net
Thu Jul 27 04:12:01 EDT 2006


mik... at optusnet.com.au wrote:
> John Machin wrote:
> > miker2 at optusnet.com.au wrote:
> > > HI,
> > >
> > > I'm having trouble writing to a MySql db using python and the MySQLdb
> > > module. Here is the code:
> > >
> > > import MySQLdb
> > > base = MySQLdb.connect(host="localhost", user="blah", passwd="blah",
> > > db="test_py")
> > > cursor = base.cursor()
> > > cursor.execute("INSERT INTO table (field) VALUES (int)")
> >
> > I've never used MySQL but they're all much the same --
> > "table" is a reserved word. What is "int" supposed to be? That's not
> > valid SQL AFAIK. Is that exactly what you typed? Or are you coyly
> > obfuscating?
> >
> > Try this:
> >     cursor.execute("INSERT INTO tablename (fieldname) VALUES (42)")
> > or better,
> >     somevar = 42
> >     cursor.execute("INSERT INTO tablename (fieldname) VALUES (?)",
> > (somevar,))
> > even better, read the docs and look at the examples :-)
> >
> >
> > >
> > > this does not work
> >
> > ... and the error message was ... what? If it's not a state secret, how
> > about divulging it?
> >
> >
> > > but the interesting thing is, there is an
> > > AUTO_INCREMENT
> > > field. Now say i had a couple of entries in there already:
> > >                                    auto  table
> > >                                     1    |    90
> > >                                     2    |    32
> > >
> > > and then i run my py script 3 times, the data is not entered but if i
> > > add
> > > another entry from mysql the auto increment field will have counted the
> > >
> > > python entries:
> > >                        auto    table
> > >                          1    |    90
> > >                          2    |    32
> > >                          6    |    47
> >
> > Evidently it's committed the auto increment before it decides that it
> > doesn't like your SQL or whatever. Read the warranty card that came
> > with the autoincrementer gizmoid; you won't find "continuous" or "no
> > gaps" mentioned anywhere.
> >
> > > please tell me what i am doing wrong.
> >
> > Inter alia, not giving enough clear unambiguous info about what your
> > problem really is.
> >
> > Cheers,
> > John
>
>
> sorry guys...
>
> forget about the auto incrementer for a second.
>
> the entry is not being recorded. that is my problem. the script does
> not work. thanks.

OK we've forgotten about the auto incrementer.

Now tell us what "does not work" means.
Show us an actual suitably-cut down script that "does not work".
If you get an error message, tell us what the error message was.
If you didn't get an error message, bloody well tell us that you
didn't.

BTW, if the script doesn't contain

    base.commit()

somewhere, take yourself out to the back lane and give yourself a good
thumping :-)
Then come back in and read the docs about transactions and commit and
autocommit etc.

HTH,
John




More information about the Python-list mailing list