在SQLAlchemy中使用筛选器选择时出现奇怪的SQL错误

时间:2012-07-13 20:11:22

标签: python sqlalchemy

编辑我找到了答案,这与模型的设置方式无关。我试图将request.form从烧瓶传递给实际创建地址的函数。显然将其转换为kwargs会导致问题。手动指定每个参数使一切正常。

我是SQLAlchemy的新手,到目前为止事情进展顺利,但我遇到了问题而且我完全被难倒了所以我希望有人在这里知道我做错了什么!

我有一个地址模型,我正在尝试从中进行选择。该模型如下所示:

class Address(Base):
    __tablename__ = 'address'

    id = Column(Integer, primary_key=True)
    comment = Column(String(255))
    address1 = Column(String(100))
    address2 = Column(String(100))
    city = Column(String(45))
    state = Column(String(2))
    zip = Column(String(5))
    zip4 = Column(String(4))
    lat = Column(Float)
    lon = Column(Float)

我正在尝试匹配通过我的应用程序输入的地址元素,看看我是否可以找到没有地理编码的纬度和经度。我在其他有效的查询中使用了filter_by,所以我在这里尝试相同的事情:( address dict来自我的应用程序的post参数)

matched_address = session.query(Address).filter_by(
    address1=address['address1'],
    city=address['city'],
    state=address['state'],
    zip=address['zip']
).one()

不幸的是,这是因为SQL错误而失败:

ProgrammingError: (ProgrammingError) (1064, 'You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right 
syntax to use near \') AND address.state = ("\'XX\'",) AND address.zip = 
("\'XXXXX\'",) AND address.city \' at line 3')

这看起来非常难看!额外的()来自哪里?我在echo=True的调用中打开了create_engine(),它打印出一个看起来正确的查询(我在mysql客户端中尝试过没有问题):

SELECT address.id AS address_id, address.comment AS address_comment, 
    address.address1 AS address_address1, address.address2 AS address_address2, 
    address.city AS address_city, address.state AS address_state, 
    address.zip AS address_zip, address.zip4 AS address_zip4, 
    address.lat AS address_lat, address.lon AS address_lon 
FROM address 
WHERE address.address1 = %s AND address.state = %s AND address.zip = %s AND 
    address.city = %s 
LIMIT %s

但它说输出的参数看起来有点奇怪,虽然我不确定这只是python对象还是它真的以这种方式进入MySQL :(注意X字符只是我真实地址的替换)

([u'1111 XXXXXX St'], [u'XX'], [u'XXXXX'], [u'XXXXX'], 1)

为了好玩,我尝试使用filter()代替filter_by(),但我得到了同样的错误:

matched_address = database.session.query(Address).filter(
    and_(Address.address1==address['address1'], Address.city==address['city'],    
    Address.state==address['state'],Address.zip==address['zip'])).one()

很抱歉,如果这是一个基本问题。在此先感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

我找到了答案,这与模型的设置方式无关。我试图将来自flask的request.form传递给实际创建地址的函数。显然将其转换为kwargs会导致问题。手动指定每个参数使一切正常。

答案 1 :(得分:0)

您是否尝试过打印address['address1']等的值? repr可能是最清楚的。

[编辑]

看了一下文档后,我想知道你是否应该这样做:

from sqlalchemy import and_
filter(and_(address1 == address['address1'], city == address['city'], ...))

答案 2 :(得分:0)

您的类定义对我有用,以您未提供的大量设置代码为模。对随后的代码量表示歉意。首先,定义:

from sqlalchemy import create_engine, Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class Address(Base):
    __tablename__ = 'address'

    id = Column(Integer, primary_key=True)
    comment = Column(String(255))
    address1 = Column(String(100))
    address2 = Column(String(100))
    city = Column(String(45))
    state = Column(String(2))
    zip  = Column(String(5))
    zip4 = Column(String(4))
    lat = Column(Float)
    lon = Column(Float)

    #note I am only initializing the querying columns. i am lazy   
    def __init__(self, cm, a1, ct, st):
        self.comment = cm
        self.address1 = a1
        self.city = ct
        self.state = st

将该类定义放入文件MyAddr.py并导入它,然后启动python。注意我在SA演示中使用sqlite。

>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite:///:memory:', echo=False)
>>> from sqlalchemy.orm import sessionmaker
>>> Session = sessionmaker(bind=engine)
>>> from MyAddr import Base, Address
>>> Base.metadata.create_all(engine)
>>> 
>>> a = '1234 Main St.'
>>> c = 'Anywhere'
>>> st= 'MO'
>>> cm= 'Blah blah blah blah blah'
>>> 
>>> addr = Address(cm, a, c, st)
>>> 
>>> s = Session()
>>> s.add(addr)
>>> s.commit()
>>> 
>>> q = s.query(Address).filter_by(address1=a,city=c)
>>> 
>>> q.one()
<op.Address object at 0x10d158a50>
>>> q = s.query(Address).filter_by(address1=a,city=c,state=st)
>>> q.one()
<op.Address object at 0x10d158a50>
>>> abc = q.one()
>>> abc
<op.Address object at 0x10d158a50>
>>> abc.city
u'Anywhere'
>>> abc.state
u'MO'
>>> abc.address1
u'1234 Main St.'

由于您的课程定义可以有效,因此您的问题(对我来说)中没有足够的信息来了解它无法正常工作的原因。也许这与您的输入数据有关?您的错误消息包含两组引号,一组是转义的。你的输入字典是否包含带引号的字符串?