web.py 连接 MySQL 错误

0 投票
2 回答
1837 浏览
提问于 2025-04-16 17:58

我在用web.py连接本地的mysql时,出现了一些错误:

Traceback (most recent call last):
  File "code.py", line 7, in <module>
    db = web.database(dbn='mysql',user='root',pw='123456',db='pytable')
  File "/Library/Python/2.6/site-packages/web.py-0.34-py2.6.egg/web/db.py", line 1078, in database
    return _databases[dbn](**params)
  File "/Library/Python/2.6/site-packages/web.py-0.34-py2.6.egg/web/db.py", line 928, in __init__
    import MySQLdb as db
  File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in <module>
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/rlog/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): no suitable image found.  Did find:
    /Users/rlog/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so: mach-o, but wrong architecture

我的Python代码是这样的:

import web
render = web.template.render('templates/')
urls = (
    "/(.*)","index"
)
app = web.application(urls,globals())
db = web.database(dbn='mysql',user='root',pw='123456',db='pytable')
class index:
    def GET(self):
        todos = db.select('todo')
        return render.index(todos)
if __name__=="__main__":app.run()

我的Python版本是2.6.1

我该怎么解决这个问题呢?

谢谢!

2 个回答

0

这段代码的意思是,创建一个连接到数据库的对象,具体使用的是MySQL数据库。这里的各个部分解释如下:

dbn -- 这是你正在使用的数据库类型,对于MySQL来说就是写“mysql”

db -- 这是数据库的名字

user -- 这是你用来登录数据库的用户名

pw -- 这是你用来登录数据库的密码

0

你安装的用于Python的mysql适配器可能损坏了,或者是为不同的系统架构准备的。我不太清楚你是怎么安装的(或者在OSX上这些东西是怎么工作的)。这里有个问题讨论了安装的相关内容。如果你能正确安装它,应该就能正常使用了(前提是你的账号信息是正确的)。

撰写回答