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 5 1205
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.
"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
..
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.
"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
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
...
|
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,...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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"...
|
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...
|
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,...
|
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...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |