2

编辑我找到了答案,它与模型的设置方式无关。我试图将request.formfrom 烧瓶传递给实际创建地址的函数。显然将其转换为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字典来自我的应用程序的发布参数)

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()

对不起,如果这是一个基本问题。提前感谢您的帮助!

4

3 回答 3

1

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

于 2012-07-31T21:22:23.150 回答
0

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

[编辑]

查看文档后,我想知道您是否应该这样做:

from sqlalchemy import and_
filter(and_(address1 == address['address1'], city == address['city'], ...))
于 2012-07-14T01:14:52.040 回答
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.'

由于您的类定义可以工作,因此(对我而言)您的问题中没有足够的信息来了解它为什么不起作用。也许这与您的输入数据有关?您的错误消息包含两组引号,一组已转义。您的输入字典是否包含带引号的字符串?

于 2012-07-14T03:28:20.110 回答