468,537 Members | 1,944 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

MySQLdb trouble

Hello there!

I got some trouble trying to insert data into the database with MySQLdb
module.

I have such code:

from MySQLdb import *

def loc_connect_db():
"""
The DB connection subroutine
"""
db = MySQLdb.connect(host = "localhost",
user = "root",
passwd="mysql",
db="some_db")
return db

db = loc_connect_db()
cursor = db.cursor()

query = """insert ignore into categories (cat_id, cat_name, parent)
values (%d, "%s", %d)""" % (int(CatId), CatName.capitalize(), int(parent))
print query
cursor.execute(query)

Queries are formed in the loop, they look fine. No errors, but no
effect, records are not inserted into the table. There is my possible
mistake?

Any suggestions are very appreciated - Thanks in advance.

Best regards,
Nicolay
May 10 '06 #1
5 1056
Nicolay A. Vasiliev wrote:
def loc_connect_db():
"""
The DB connection subroutine
"""
db = MySQLdb.connect(host = "localhost",
user = "root",
passwd="mysql",
db="some_db")
return db


Hi. Sorry I can't help, but I'm interested in learning about mysqldb and
I was wondering why you chose to wrap the 'connect' function inside your
own function. Does this accomplish something that I'm not seeing?
Couldn't you just use the body of the loc_connect_db() function as your
actual code in order to get 'db'?

Thanks.
May 10 '06 #2
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:Xn******************@news.tufts.edu...
Nicolay A. Vasiliev wrote:
def loc_connect_db():
"""
The DB connection subroutine
"""
db = MySQLdb.connect(host = "localhost",
user = "root",
passwd="mysql",
db="some_db")
return db
Hi. Sorry I can't help, but I'm interested in learning about mysqldb and
I was wondering why you chose to wrap the 'connect' function inside your
own function.

Does this accomplish something that I'm not seeing?
Probably!

1) His code body will be less likely to cause migrane headaches when he
tries to read and interpret what he did a year from now. If you are trying
to figure out what is going on with the logic, user names and passwords can
be so much chaff your brain needs to wade through in order to get to the
central idea.

2) The coder won't have to repeat himself if he needs to re-open the
databases.
He can just call his less comples loc_connect_db() function.

And know that #1 is a sufficient reason even if #2 doesn't apply!
Couldn't you just use the body of the loc_connect_db() function as your
actual code in order to get 'db'?


Yes you could!
It's really a matter of style and preference. Some programmers (myself
included!) prefer many, very short and simple functions over fewer function
with longer blocks of more complex code.

It's hard to make a mistake by having too many short and simple functions.

And much too easy to make them when you have too few ;-)
Thomas Bartkus

..
May 10 '06 #3
Thomas Bartkus wrote:
1) His code body will be less likely to cause migrane headaches when he
tries to read and interpret what he did a year from now. If you are trying
to figure out what is going on with the logic, user names and passwords can
be so much chaff your brain needs to wade through in order to get to the
central idea.

2) The coder won't have to repeat himself if he needs to re-open the
databases.


Ah, that makes sense! It seemed like an unnecessary step, but in some
ways it might end up being more efficient.
May 10 '06 #4

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:U_******************@news.tufts.edu...
Thomas Bartkus wrote:
1) His code body will be less likely to cause migrane headaches when he
tries to read and interpret what he did a year from now. If you are trying to figure out what is going on with the logic, user names and passwords can be so much chaff your brain needs to wade through in order to get to the
central idea.

2) The coder won't have to repeat himself if he needs to re-open the
databases.


Ah, that makes sense! It seemed like an unnecessary step, but in some
ways it might end up being more efficient.


For the machine - it *is* an unnecessary step.
For the human being who must write and maintain code, it is quite a useful
step.

And a very excellent coding habit to get into ;-)
Thomas Bartkus
May 10 '06 #5
Hi!
Number two suggestion: try committing the transaction (autocommit
might not be enabled in your environment).


commit() - is what I need :)

Thank you very much!
May 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dave Harrison | last post: by
5 posts views Thread by Chris Stromberger | last post: by
1 post views Thread by Sheila King | last post: by
1 post views Thread by Peter Nikolaidis | last post: by
4 posts views Thread by Perflubron | last post: by
reply views Thread by nephish | last post: by
1 post views Thread by Steve | last post: by
1 post views Thread by Aljosa Mohorovic | last post: by
2 posts views Thread by Nikhil | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.