【问题标题】:SQL Alchemy: SQL syntax error on add with sessionSQL Alchemy:添加会话时出现 SQL 语法错误
【发布时间】:2013-08-16 23:07:10
【问题描述】:

当我尝试使用带有 SQLAlchemy 的会话执行添加时,我目前遇到以下错误。

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 '%s)' at line 1") b'INSERT INTO ctr_status (`Name`) VALUES (%s)' ('Test',)

代码如下:

from sqlalchemy import MetaData, Table
from sqlalchemy import create_engine, orm

# Login omitted...

url = 'mysql+mysqldb://{user}:{password}@{host}:{port}/{database}'.format(user=user, password=password, host=host, port=port, database=database)
e = create_engine(url, echo=True)
metadata = MetaData()
metadata.bind = e
metadata.create_all()
ctr_status = Table('ctr_status', metadata, autoload=True)

class CTRStatus(object):
    pass
orm.mapper(CTRStatus, ctr_status)
new_status = CTRStatus()
new_status.Name = 'Test'

session_maker = orm.sessionmaker(bind=e, autoflush=True, autocommit=False, expire_on_commit=True)
session = orm.scoped_session(session_maker)
session.add(new_status)

# Crash here at flush
session.flush()

我对自己做错了什么感到困惑。我目前正在使用 MySQL 5.5Python 3.3.2SQLAlchemy-0.8.2.win32-py3.3。

以下是我一直关注的一些链接:

SQL Alchemy - Tutorial

SQL Alchemy - Sessions

【问题讨论】:

  • 我怀疑是 DBAPI 适配器。看来您正在使用 MySQL-python。它支持 Python 3 吗?你能试试另一个适配器吗? MySQL-Connector、cymysql 还是 OurSQL?

标签: python sqlalchemy


【解决方案1】:

我设法通过将适配器切换到 OurSQL 并将 url 更改为使用 mysql+oursql 来使其工作。

我想补充一点,让 OurSQL 在我的 Python 3.3 设置上工作令人惊讶地不平凡。

  1. 我必须从初始下载的文件中删除 oursqlx/oursql.c,以便 Cython 自动生成它。
  2. 我还必须手动修改 setup.py,因为它在 setup_windowsish() 中为 mysql_root 变量指向了错误的注册表路径。本质上,当前的实现似乎不支持 MySQL Server 5.5,也不支持注册表路径在 HKEY_CURRENT_USER 而不是 HKEY_LOCAL_MACHINE 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-31
    • 2015-08-19
    • 2014-11-17
    • 2013-10-07
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    相关资源
    最近更新 更多