MySql

John Machin sjmachin at lexicon.net
Thu Jul 27 03:42:33 EDT 2006


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




More information about the Python-list mailing list