“SELECT LAST_INSERT_ID()”处出现语法错误

2024-06-16 12:27:19 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我用python运行查询,它将返回一个语法错误。在

但是,如果我用navicat运行这个,它就可以了。在

为什么它不在python中运行?在

sql查询

INSERT INTO
    solarsystem
    (
        solarsystemName,
        solarsystemPositionX,
        solarsystemPositionY,
        solarsystemSectorId,
        solarsystemAngle
    ) 
VALUES 
    ('Lima [698/562]',698,562,13,171);

SELECT LAST_INSERT_ID();

错误

1064, u"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 'SELECT LAST_INSERT_ID()'


[编辑]

创建.py

^{pr2}$

更新

如果我尝试将INSERT作为单个查询,这就可以了。在

但是,SELECT LAST_INSERT_ID();没有起作用。它返回:

TypeError: not all arguments converted during string formatting


Tags: thetoidsqlyourselectlastinsert
1条回答
网友
1楼 · 发布于 2024-06-16 12:27:19

在连接上执行此操作以获取插入到游标对象上的最后一行ID

你可以用

connection.insert_id()

或者如果你有光标:

^{pr2}$

编辑

对于sqlalchemy,您可以使用:

with self.engine.begin() as connection:
        result_proxy = connection.execute(sql_query, args).first()
        last_id=result_proxy.inserted_primary_key[0]

你的身份证在last_id

但是必须从sql查询中删除SELECT LAST_INSERT_ID();

相关问题 更多 >