如果您这样做,SQLite3会做您期望的事情:
select "foo" from bar;但是,如果标识符不存在:
select "non-existant" from bar;它回过头来(为了与旧版本的兼容性,我猜)将引用的文本视为字符串。
这会给我带来问题,因为我使用这样的引用列动态地创建查询,而后一种行为则返回无意义的结果,而不是抛出错误。
我正在编写python代码,并使用一个封装PEP-249 Python数据库API规范v2.0模块的模块,这样我就可以在必要时输入特定于数据库的黑客。
我们的后端数据库可能会发生变化(实际上,在某些时候,本地测试和生产可能会有所不同),所以如果可能的话,我希望保持SQL本身的标准。
我能不能:
发布于 2013-01-23 15:02:15
如果列名以表名或别名作为前缀,则不能误解它们:
SELECT bar."foo", a."baz" FROM bar, blah AS a当您处理多个表时,可能需要使用它来避免列名冲突。
发布于 2013-01-23 14:00:14
不要使用引号,使用[和]
sqlite> create table blah (name text);
sqlite> select "something" from blah;
sqlite> select "name" from blah;
sqlite> insert into blah values ('hello');
sqlite> select "something" from blah;
something
sqlite> select "name" from blah;
hello
sqlite> select [something] from blah;
Error: no such column: something
sqlite> select [name] from blah;
hello
sqlite> 试图以编程方式实现:
import re
from itertools import cycle
s = 'select "something" from blah;'
sel, cols, rest = re.match(r'(select)\s+(.*?)\s+(from\s+.*)', s).groups()
cols = re.sub('"', lambda g, I=cycle('[]'): next(I), cols)
print ' '.join( (sel, cols, rest) )
# select [something] from blah;https://stackoverflow.com/questions/14481456
复制相似问题