472,777 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,777 software developers and data experts.

Problem with global

Hello,
I have a little problem with the global statement.

def executeSQL(sql, *args):
try:
import pdb; pdb.set_trace()
cursor = db.cursor() # db is <type 'NoneType'>.
[...]
except:
print "Problem contacting MySQL database. Please contact root."
sys.exit(-1)
db = None # Global Variable for DB connection

def main():
[...]
global db
db = MySQLdb.connect(...)
[...]
executeSQL(sql, args)
Why isn't the global variable db not written in main() to be a mysql
connection and still none type in executeSQL?

Thanks,

Florian
Oct 12 '07 #1
2 1553
Florian Lindner wrote:
Hello,
I have a little problem with the global statement.

def executeSQL(sql, *args):
try:
import pdb; pdb.set_trace()
cursor = db.cursor() # db is <type 'NoneType'>.
[...]
except:
print "Problem contacting MySQL database. Please contact root."
sys.exit(-1)
db = None # Global Variable for DB connection

def main():
[...]
global db
db = MySQLdb.connect(...)
[...]
executeSQL(sql, args)
Why isn't the global variable db not written in main() to be a mysql
connection and still none type in executeSQL?

Thanks,

Florian
Because you have it to let executeSQL know that it is global or it creates a
local copy in local namespace.

def executeSQL(sql, *args):
global db
try:
import pdb; pdb.set_trace()
cursor = db.cursor() # db is <type 'NoneType'>.
[...]
except:
print "Problem contacting MySQL database. Please contact root."
sys.exit(-1)

-Larry
Oct 12 '07 #2
Larry Bates wrote:
Florian Lindner wrote:
>Hello,
I have a little problem with the global statement.

def executeSQL(sql, *args):
try:
import pdb; pdb.set_trace()
cursor = db.cursor() # db is <type 'NoneType'>.
[...]
except:
print "Problem contacting MySQL database. Please contact root."
sys.exit(-1)
db = None # Global Variable for DB connection

def main():
[...]
global db
db = MySQLdb.connect(...)
[...]
executeSQL(sql, args)
Why isn't the global variable db not written in main() to be a mysql
connection and still none type in executeSQL?

Thanks,

Florian

Because you have it to let executeSQL know that it is global or it creates
a local copy in local namespace.
That's not right in the context because db is read before it written.
Therefore the global copy springs into the local namespace.
def executeSQL(sql, *args):
global db
try:
import pdb; pdb.set_trace()
cursor = db.cursor() # db is <type 'NoneType'>.
[...]
except:
print "Problem contacting MySQL database. Please contact root."
sys.exit(-1)
I've solved it. It was a problem you could not have possibly seen. Actually
in my script executeSQL is called before db = MySQLdb.connect(..) is
called. When I have simplified the code for the posting I've changed it
made it right without knowing.

Regards,

Florian
Oct 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bartosz Wegrzyn | last post by:
I need help with sessions. I createt set of web site for nav with authorization. first I go into main.php which looks like this: <?php //common functions include_once '../login/common.php';...
1
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that...
3
by: Steven Fox | last post by:
============================================================ About DB2 Administration Tools Environment ============================================================ DB2 administration tools level:...
4
by: zubair | last post by:
Hello everyone! I have uploaded my site on a webserver. Some times it works fine but some time it gives error "Null Object reference exception". The same site on my local server works just fine. ...
7
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user...
11
by: Ron | last post by:
I have a web project compiled with the new "Web Deployment Projects" plugin for VS2005. I'm deploying the web project to one assembly and with updateable option set to ON. When I'm running the...
23
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
6
by: Royan | last post by:
Ok the problem is quite hard to explain, but i'll try to keep it as simple as i can. Imagine I have the following structure of my files and folders: /root/global.inc |__/files/foo.php...
1
by: Brock | last post by:
Thanks in advance... (you can see a screenshot of what my form looks like currently at http://www.juggernautical.com/DataGrid.jpg - the Datalist is super-imposed in 'design view' but the DataGrid...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.