简体   繁体   中英

SQLAlchemy Select statement - SQL Syntax Error

I have created a table (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()

But when I try to query it:

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)

I get an error on the result = db.execute(s) line indicating the following:

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

I'm obviously new to SQLAlchemy, and I have no idea what I'm doing wrong, despite having googled every tutorial on the web and changed this a million times. Any help?

I suspect Table.select() is only for selecting specific columns. For SELECT * , the expression language tutorial uses this syntax instead:

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

您的feeds.select()调用中可能缺少一些内容,我将再次查看此功能的API文档。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM