sql python - return sql variable
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()
精彩评论