gpt4 book ai didi

python - SQLAlchemy Select 语句 - SQL 语法错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:18 27 4
gpt4 key购买 nike

我已经创建了一个表(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 的新手,而且我不知道自己做错了什么,尽管我已经在网上搜索了每个教程并将其更改了一百万次。有帮助吗?

最佳答案

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

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

关于python - SQLAlchemy Select 语句 - SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com