1

我有一个名为 my_legacy_db 的遗留数据库,它与普通数据库分开。

my_legacy_db

用户 - 电子邮件 - 用户名 - 姓名

因此,悬崖,您的第一部分将生成字段名称并将所有内容放入字典中以构建查询。问题是当我执行此查询时:

db().select(my_legacy_db.users)

I get this error:
In [20] : db().select(my_legacy_db.users)
Traceback (most recent call last):
  File "/opt/web-apps/web2py/gluon/contrib/shell.py", line 233, in run
    exec compiled in statement_module.__dict__
  File "<string>", line 1, in <module>
  File "/opt/web-apps/web2py/gluon/dal.py", line 7578, in select
    return adapter.select(self.query,fields,attributes)
  File "/opt/web-apps/web2py/gluon/dal.py", line 1307, in select
    sql = self._select(query, fields, attributes)
  File "/opt/web-apps/web2py/gluon/dal.py", line 1196, in _select
    raise SyntaxError, 'Set: no tables selected'
SyntaxError: Set: no tables selected

In [21] : print (flickr_db.users)
users

In [22] : print flickr_db
<DAL {'_migrate_enabled': True, '_lastsql': "SET sql_mode='NO_BACKSLASH_ESCAPES';", '_db_codec': 'UTF-8', '_timings': [('SET FOREIGN_KEY_CHECKS=1;', 0.0002460479736328125), ("SET sql_mode='NO_BACKSLASH_ESCAPES';", 0.00025606155395507812)], '_fake_migrate': False, '_dbname': 'mysql', '_request_tenant': 'request_tenant', '_adapter': <gluon.dal.MySQLAdapter object at 0x91375ac>, '_tables': ['users'], '_pending_references': {}, '_fake_migrate_all': False, 'check_reserved': None, '_uri': 'mysql://CENSORED', 'users': <Table 'username': <gluon.dal.Field object at 0x9137b6c>, '_db': <DAL {...}>, 'cycled': <gluon.dal.Field object at 0x94d0b8c>, 'id': <gluon.dal.Field object at 0x95054ac>, 'ALL': <gluon.dal.SQLALL object at 0x969a7ac>, '_sequence_name': 'users_sequence', 'name': <gluon.dal.Field object at 0x9137ecc>, '_referenced_by': [], '_singular': 'Users', '_common_filter': None, '_id': <gluon.dal.Field object at 0x95054ac>}>, '_referee_name': '%(table)s', '_migrate': True, '_pool_size': 0, '_common_fields': [], '_uri_hash': 'dfb3272fc537e3339819a1549180722e'}>

我在这里做错了吗?遗留数据库不是内置在 /databases 中吗?提前感谢您的帮助。

更新:我尝试了模型外壳中建议的安东尼:

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

现在我知道用户是在 my_legacy_db 中定义的,并且所有语法都是正确的。这是因为 db 文件没有正确生成而出现的错误吗?还是我仍然对选择语法做错了什么?

4

1 回答 1

1

如果“用户”是表的名称,并且您想选择所有记录和所有字段,您可以:

db(my_legacy_db.users).select()

查询进入内部db(),而不是内部select()select()列出要返回的字段,如果需要所有字段,则将其留空)。请注意,在上面的行中,my_legacy_db.users实际上并不是一个查询,而只是一个表——这是告诉 web2py 你想要表中的所有记录的快捷方式。

你也可以这样做:

db().select(my_legacy_db.users.ALL)

这表明您需要所有字段,并且通过排除查询,它假定您需要表中的所有记录。

有关详细信息,请参阅本书。

于 2012-04-20T18:36:52.430 回答