Re: MySql



mik...@xxxxxxxxxxxxxxx wrote:
John Machin wrote:
miker2@xxxxxxxxxxxxxxx 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

.



Relevant Pages

  • Re: MySQL vs. DBI/DBD
    ... A simple Google search on the error message: ... > When I run the script I get this error message: ... consider upgrading MySQL client at test.pl line 15 ...
    (perl.dbi.users)
  • Re: not able to connect to mysql
    ... -cgi script on command line its working fine. ... Can't connect to local MySQL server through socket ... the end of that error message. ...
    (comp.lang.perl.misc)
  • Re: MySql
    ... I'm having trouble writing to a MySql db using python and the MySQLdb ... Evidently it's committed the auto increment before it decides that it ...
    (comp.lang.python)
  • Re: MySql
    ... I'm having trouble writing to a MySql db using python and the MySQLdb ... Evidently it's committed the auto increment before it decides that it ...
    (comp.lang.python)
  • Re: get the next auto increment
    ... I am using MYSql but I will try it tomorrow to see if it works for ... I guess you are using SQL Server.. ... How can I execute an insert statement into a table and get the ... > new id number that auto increment at the same time. ...
    (microsoft.public.dotnet.framework.adonet)