Web2py查询旧数据库

时间:2012-04-20 16:47:25

标签: python data-access-layer web2py legacy

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

my_legacy_db

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

所以悬崖,你的第一部分将用于生成字段名称并将所有内容放入dict中以构建查询。问题在于我执行此查询时:

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中吗?提前感谢您的帮助。

更新:我尝试在模型shell中建议使用anthony:

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文件没有正确生成?或者我仍然在选择语法时出错?

1 个答案:

答案 0 :(得分:1)

如果“users”是表的名称,并且您想要选择所有记录和所有字段,则可以执行以下操作:

db(my_legacy_db.users).select()

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

你也可以这样做:

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

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

有关详细信息,请参阅book