471,355 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 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 1508
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Bartosz Wegrzyn | last post: by
3 posts views Thread by Steven Fox | last post: by
4 posts views Thread by zubair | last post: by
7 posts views Thread by Ankit Aneja | last post: by
1 post views Thread by Brock | last post: by

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.