473,549 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.capital ize(), 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 1206
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******@NOSPA Mgmail.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******@NOSPA Mgmail.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2091
by: Dave Harrison | last post by:
Hi all, got a problem combinging mx and MySQLdb, when I build and install both for my Python2.1 install on a Solaris 9 box I can import mx fine, but importing MySQLdb causing python to core dump. I am using egenenix 2.0.4 and MySQLdb 0.9.2. I have also replaced the -shared flag in the Makefile is /usr/local/lib/Python2.1/config with -G (a...
5
5075
by: Chris Stromberger | last post by:
When issuing updates in mysql (in the console window), mysql will tell you if any rows matched and how many rows were updated (see below). I know how to get number of rows udpated using MySQLdb, but is there any way to get the number of rows matched? I want to find out, when rows updated = 0, if there were no updates because the row wasn't...
1
6808
by: Sheila King | last post by:
I have searched for an answer to this question on the comp.lang.python archives at Google Groups, and also looked through the (sparse) MySQLdb documentation, and cannot confirm for a FACT what I think I know. Looking for affirmation before I go "assuming" something and getting myself into trouble somewhere along the line... Here is what I...
1
2577
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to "conflicting header files," but I don't know what to do about conflicting header files, or where I would find them, and once I found them, which ones to...
4
6439
by: Perflubron | last post by:
Hi, I'm having trouble installing MySQLdb 0.9.2 on the following Cygwin system: - Python 2.2.2 - MySQL 3.23.57 - gcc 2.95.3-10 The steps I take are: 1. Modify setup.py - Set thread_safe_library = NO
0
1041
by: nephish | last post by:
Hey there all, i have a question about how to point my python install to my sql database. when i enter this: db = MySQLdb.connect(user="user", passwd="pass", db="myDB") i get this: Traceback (most recent call last): File "<pyshell#1>", line 1, in -toplevel- db = MySQLdb.connect(user="user", passwd="pass", db="MyDB")
1
2720
by: Steve | last post by:
Darwin steve.local 8.3.0 Darwin Kernel Version 8.3.0: Mon Oct 3 20:04:04 PDT 2005; root:xnu-792.6.22.obj~2/RELEASE_PPC Power Macintosh powerpc MacOSX 10.4.3 mysql Ver 14.7 Distrib 4.1.14, for apple-darwin8.2.0 (powerpc) using readline 4.3 runing the software gives me steve:~/MySQL-python-1.2.0 steve$ python setup.py build
1
1696
by: Aljosa Mohorovic | last post by:
i have a working MySQLdb module (/usr/lib/python2.4/site-packages/ MySQL_python-1.2.2-py2.4-linux-i686.egg), using it without problems. "clean shell" after login: python -c "import MySQLdb" reports no errors if i export PYTHONPATH: export PYTHONPATH=/var/www/projects/uv_portal/portal python -c "import MySQLdb" reports no errors as in...
2
4311
by: Nikhil | last post by:
I am using the MySQLdb python module. I have a table named 'testing' with few columns, under the 'test' database, what is hosted on a remote mysql server. I want to run the following query to get a comma-seperated information from the table LOCK TABLES foo READ; SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
0
7546
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7985
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7830
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6071
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1082
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
784
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.