python2.6 与 MySQLdb,NameError 'MySQLdb'未定义
从口译员中我可以发出>>>来自 MySQLdb 就好了。所以,我假设该模块确实加载了。我的来源如下:
从 Tkinter 导入 *
从 MySQLdb 导入 *
“”“
经济实惠的拖车的库存控制功能:
connection() - 控制数据库连接
delete() - 从数据库中删除项目
edit() - 编辑数据库中项目的属性
Lookup() - 查找项目
new() - 将新项目添加到数据库
receive() - 增加数据库中项目的数量
remove() - 减少数据库中项目的数量
report() - 显示库存活动
Transfer() - 从一个位置删除项目,在另一个位置接收项目“”“
def 控制():
....dbInfo = { '用户名':'livetaor_atowtw', '密码':'垃圾邮件', \
....'server':'eggs.com', 'base':'livetaor_towing', 'table':'inventory' }
....def testConnection():
........sql = MySQLdb.connect(user=dbInfo[用户名], passwd=dbInfo[密码], \
........host=dbInfo[服务器], db=dbInfo[基])
........MySQLdb.mysql_info(sql)....testConnection()
控制()
这给了我:
brad@brads-debian:~/python/towing/inventory$ python inventory.py
回溯(最近一次调用最后一次):
..文件“inventory.py”,第 53 行,位于
....control()
..文件“inventory.py”,第 26 行,在控制中
....testConnection()
..文件“inventory.py”,第 22 行,位于 testConnection
....sql = MySQLdb.connect(user=dbInfo[用户名], passwd=dbInfo[密码], \
NameError:未定义全局名称“MySQLdb”
1) 我哪里出错了?
2)你们还看到其他什么问题吗?
3)关于如何检查与数据库(不仅仅是服务器)的有效连接有什么建议吗?
from the interpreter i can issue >>> from MySQLdb just fine. so, I'm assuming the module did actually load. My source looks as follows:
from Tkinter import *
from MySQLdb import *
"""
Inventory control for Affordable TowingFunctions:
connection() - Controls database connection
delete() - Remove item from database
edit() - Edit item's attributes in database
lookup() - Lookup an item
new() - Add a new item to database
receive() - Increase quantity of item in database
remove() - Decrease quantity of item in database
report() - Display inventory activity
transfer() - Remove item from one location, receive item in another"""
def control():
....dbInfo = { 'username':'livetaor_atowtw', 'password':'spam', \
....'server':'eggs.com', 'base':'livetaor_towing', 'table':'inventory' }
....def testConnection():
........sql = MySQLdb.connect(user=dbInfo[username], passwd=dbInfo[password], \
........host=dbInfo[server], db=dbInfo[base])
........MySQLdb.mysql_info(sql)....testConnection()
control()
this gives me:
brad@brads-debian:~/python/towing/inventory$ python inventory.py
Traceback (most recent call last):
..File "inventory.py", line 53, in
....control()
..File "inventory.py", line 26, in control
....testConnection()
..File "inventory.py", line 22, in testConnection
....sql = MySQLdb.connect(user=dbInfo[username], passwd=dbInfo[password], \
NameError: global name 'MySQLdb' is not defined
1) where am I going wrong?
2) any other gotcha's that you folks see?
3) any advice on how to check for a valid connection to the database, (not just the server)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是由于您导入模块然后引用它的方式造成的。
更改:
如果
您打算参考您的时尚风格。
无论如何,这些语句和函数的工作原理如下:
来自: http://effbot.org/zone/import -confusion.htm
import X 导入模块 X,并在当前命名空间中创建对该模块的引用。或者换句话说,运行此语句后,您可以使用 X.name 来引用模块 X 中定义的内容。
from X import * 导入模块 X,并在当前命名空间中创建对由该模块(即名称不以“_”开头的所有内容)。或者换句话说,运行此语句后,您可以简单地使用普通名称来引用模块 X 中定义的内容。但 X 本身并未定义,因此 X.name 不起作用。如果名称已经定义,它将被新版本替换。如果 X 中的名称更改为指向某个其他对象,您的模块将不会注意到。
from X import a, b, c 导入模块 X,并在当前命名空间中创建对给定对象的引用。或者换句话说,您现在可以在程序中使用 a 和 b 和 c。
This is due to the way you're importing the module and then referencing it.
Change:
to
if you plan on referencing it the fashion that you are.
Anyway, here’s how these statements and functions work:
From: http://effbot.org/zone/import-confusion.htm
import X imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can use X.name to refer to things defined in module X.
from X import * imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so X.name doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.
from X import a, b, c imports the module X, and creates references in the current namespace to the given objects. Or in other words, you can now use a and b and c in your program.
from MySQLdb import *
和import MySQLdb
做非常不同的事情。from MySQLdb import *
andimport MySQLdb
do very different things.