MySQLdb trouble

  • Thread starter Nicolay A. Vasiliev
  • Start date
N

Nicolay A. Vasiliev

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
 
D

Dennis Lee Bieber

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?
Number one suggestion: DON'T DO THAT!

Use:

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

The DB-API standard is that the .execute() method will correctly
escape/quote the data items supplied in the second argument tuple (and
MySQLdb, as I recall, uses %s as the parameter marker -- others may use
?)
Any suggestions are very appreciated - Thanks in advance.
Well... "ignore" says that any record that duplicates primary keys
will just be... ignored... not entered. So... are you sure you haven't
duplicated the primary key?

Number two suggestion: try committing the transaction (autocommit
might not be enabled in your environment).
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Salerno

Nicolay said:
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.
 
T

Thomas Bartkus

John Salerno said:
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

..
 
J

John Salerno

Thomas said:
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.
 
T

Thomas Bartkus

John Salerno said:
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
 
N

Nicolay A. Vasiliev

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

MySQLdb 2
MySQLdb not playing nice with unicode 1
MySQLdb insert HTML code error 0
MySQLdb 3
MySQLdb compare lower 2
Trouble with MySQLdb 1
MySqlDb any way to see the query string 3
MySQLdb problem 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top