gpt4 book ai didi

python - web2py 的 'image blog' 上的简单 select 语句出现错误

转载 作者:行者123 更新时间:2023-11-30 23:44:08 25 4
gpt4 key购买 nike

对于我的项目,我需要连接到多个数据库并从中获取信息。我不认为这会是 web2py 的问题,但事实确实如此。我想也许我需要从头开始重建数据库,但仍然有问题。最后,我完成了介绍性的“图像”教程并将其更改为使用备用 mysql 数据库。我仍然遇到同样的错误,下面是代码:

db.py

db = DAL("mysql://root:@localhost/web2py")
images_db = DAL("mysql://root:@localhost/images_test")


images_db.define_table('image',
Field('title', unique=True),
Field('file', 'upload'),
format = '%(title)s')


images_db.define_table('comment',
Field('image_id', images_db.image),
Field('author'),
Field('email'),
Field('body', 'text'))

然后我进入“图像”的管理页面,单击“ Controller ”下的“shell”链接并执行以下操作:(在我进入索引页面生成“图像”之后:

外壳:

In [1] : print db(images_db.image).select()
Traceback (most recent call last):
File "/home/cody/Downloads/web2py/gluon/contrib/shell.py", line 233, in run
exec compiled in statement_module.__dict__
File "<string>", line 1, in <module>
File "/home/cody/Downloads/web2py/gluon/dal.py", line 7577, in select
fields = adapter.expand_all(fields, adapter.tables(self.query))
File "/home/cody/Downloads/web2py/gluon/dal.py", line 1172, in expand_all
for field in self.db[table]:
File "/home/cody/Downloads/web2py/gluon/dal.py", line 6337, in __getitem__
return dict.__getitem__(self, str(key))
KeyError: 'image'


In [2] : print images_db.has_key('image')
True


In [3] : print images_db
<DAL {'_migrate_enabled': True, '_lastsql': "SET sql_mode='NO_BACKSLASH_ESCAPES';", '_db_codec': 'UTF-8', '_timings': [('SET FOREIGN_KEY_CHECKS=1;', 0.00017380714416503906), ("SET sql_mode='NO_BACKSLASH_ESCAPES';", 0.00016808509826660156)], '_fake_migrate': False, '_dbname': 'mysql', '_request_tenant': 'request_tenant', '_adapter': <gluon.dal.MySQLAdapter object at 0x2b84750>, '_tables': ['image', 'comment'], '_pending_references': {}, '_fake_migrate_all': False, 'check_reserved': None, '_uri': 'mysql://root:@localhost/images_test', 'comment': <Table {'body': <gluon.dal.Field object at 0x2b844d0>, 'ALL': <gluon.dal.SQLALL object at 0x2b84090>, '_fields': ['id', 'image_id', 'author', 'email', 'body'], '_sequence_name': 'comment_sequence', '_plural': 'Comments', 'author': <gluon.dal.Field object at 0x2b84e10>, '_referenced_by': [], '_format': None, '_db': <DAL {...}>, '_dbt': 'applications/images/databases/e1e448013737cddc822e303fe20f8bec_comment.table', 'email': <gluon.dal.Field object at 0x2b84490>, '_trigger_name': 'comment_sequence', 'image_id': <gluon.dal.Field object at 0x2b84050>, '_actual': True, '_singular': 'Comment', '_tablename': 'comment', '_common_filter': None, 'virtualfields': [], '_id': <gluon.dal.Field object at 0x2b84110>, 'id': <gluon.dal.Field object at 0x2b84110>, '_loggername': 'applications/images/databases/sql.log'}>, 'image': <Table {'ALL': <gluon.dal.SQLALL object at 0x2b84850>, '_fields': ['id', 'title', 'file'], '_sequence_name': 'image_sequence', 'file': <gluon.dal.Field object at 0x2b847d0>, '_plural': 'Images', 'title': <gluon.dal.Field object at 0x2b84610>, '_referenced_by': [('comment', 'image_id')], '_format': '%(title)s', '_db': <DAL {...}>, '_dbt': 'applications/images/databases/e1e448013737cddc822e303fe20f8bec_image.table', '_trigger_name': 'image_sequence', '_loggername': 'applications/images/databases/sql.log', '_actual': True, '_tablename': 'image', '_common_filter': None, 'virtualfields': [], '_id': <gluon.dal.Field object at 0x2b848d0>, 'id': <gluon.dal.Field object at 0x2b848d0>, '_singular': 'Image'}>, '_referee_name': '%(table)s', '_migrate': True, '_pool_size': 0, '_common_fields': [], '_uri_hash': 'e1e448013737cddc822e303fe20f8bec'}>

现在我不太明白为什么我会在这里收到错误,一切似乎都按顺序进行。我以为web2py支持多个数据库?我做错了吗? appadmin 工作正常,也许我会编辑它并让它在生成的代码中引发错误...任何帮助将不胜感激。

  • 科迪

更新:

我刚刚尝试过这个:

模型/DB.PY

db = DAL("mysql://root:@localhost/web2py")

images_db = DAL("mysql://root:@localhost/images_test")


images_db.define_table('image',
Field('title', unique=True),
Field('file', 'upload'),
format = '%(title)s')


images_db.define_table('comment',
Field('image_id', images_db.image),
Field('author'),
Field('email'),
Field('body', 'text'))

Controller /DEFAULT.PY

def index():
"""
example action using the internationalization operator T and flash
rendered by views/default/index.html or views/generic.html
"""
if images_db.has_key('image'):
rows = db(images_db.image).select()
else:
rows = 'nope'
#rows = dir(images_db)
return dict(rows=rows)

VIEWS/DEFAULT/INDEX.HTML

{{left_sidebar_enabled,right_sidebar_enabled=False,True}}
{{extend 'layout.html'}}


these are the rows:
{{=rows }}

再次对这一切感到非常困惑。感谢任何帮助。

最佳答案

如果要查询images_db连接,则必须调用images_db(),而不是db()。所以,它会是:

images_db(images_db.image).select()

关于python - web2py 的 'image blog' 上的简单 select 语句出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com