SQLAlchemy Select语句 - SQL语法错误

时间:2010-09-13 01:00:02

标签: python sql sqlalchemy

我创建了一个表(MySQL 5.1)

from sqlalchemy import *

def get():
    db = create_engine('mysql://user:password@localhost/database')
    db.echo = True
    metadata = MetaData(db)

    feeds = Table('feeds', metadata,
            Column('id', Integer, primary_key=True),
            Column('title', String(100)),
            Column('link', String(255)),
            Column('description', String(255)),
    )

    entries = Table('entries', metadata,
            Column('id', Integer, primary_key=True),
            Column('fid', Integer),
            Column('url', String(255)),
            Column('title', String(255)),
            Column('content', String(5000)),
            Column('date', DateTime),
    )
    feeds.create()
    entries.create()

但是当我尝试查询它时:

from sqlalchemy import *
db = create_engine('mysql://user:password@localhost/database')
metadata = MetaData(db)
feeds = Table('feeds', metadata)
s = feeds.select()
result = db.execute(s)

我在result = db.execute(s)行上出现错误,指示以下内容:

sqlalchemy.exc.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 'FROM feeds' at line 2") 'SELECT  \nFROM feeds' ()

我显然是SQLAlchemy的新手,我不知道我做错了什么,尽管已经在网上搜索了每一个教程并且改变了一百万次。有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

我怀疑Table.select()仅用于选择特定列。对于SELECT *expression language tutorial使用以下语法:

from sqlalchemy.sql import select
s = select([feeds])
result = db.execute(s)

答案 1 :(得分:0)

你的feeds.select()调用可能缺少一些东西,我还要再看看这个函数的API文档。