我有一个名为my_legacy_db的遗留数据库,它与普通数据库是分开的。
my_legacy_db
用户-电子邮件-用户名-名称
所以cliff,你的第一部分工作是生成字段名,并将所有内容放在字典中来构建查询,问题是当我执行以下查询时:
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中的建议:
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文件没有正确生成而出现的错误吗?或者我的select语法仍然有问题吗?
发布于 2012-04-21 02:36:52
如果"users“是一个表的名称,并且您想要选择所有记录和所有字段,则应执行以下操作:
db(my_legacy_db.users).select()查询在db()中进行,而不是在select()中(select()是您列出希望返回的字段的位置,如果您想要所有字段,则将其保留为空)。注意,在上面的代码行中,my_legacy_db.users实际上不是一个查询,而只是一个表--这是告诉web2py您需要表中所有记录的快捷方式。
您还可以执行以下操作:
db().select(my_legacy_db.users.ALL)这表明您需要所有字段,通过排除查询,它假定您需要表中的所有记录。
有关更多详细信息,请参阅book。
https://stackoverflow.com/questions/10250279
复制相似问题