开发者

sql python - return sql variable

开发者 https://www.devze.com 2023-04-12 08:24 出处:网络
I have SQL command something like this: DEFINE item_id_variable BEGIN INSERT INTO items (name, value, status)

I have SQL command something like this:

DEFINE item_id_variable
BEGIN
INSERT INTO items (name, value, status) 
    VALUES (myname, myvalue, mystatus) 
    RETURNING item_id INTO item_id_variable;

The table has auto-incrementing attribute ite开发者_如何学运维m_id and it's value for the row I just added is in the item_id_variable. I trigger this with cursor.execute() and it works ok, but I need to read the value of "item_id_variable" into a python variable, so I can pass it further in the code. How do I do that??


Have you tried cursor.fetchall() (for example)?


Read it How do I get the "id" after INSERT into MySQL database with Python?

You can get the last insert id by "SELECT last_insert_id()" or connection.insert_id()

0

精彩评论

暂无评论...
验证码 换一张
取 消