存储过程获取最后插入的行 python mysql 连接器
stored procedure get last inserted row python mysql connector
我正在通过存储过程插入数据,并试图获取最后插入的 ID。正在插入数据。但是当我尝试 return 最后一个 ID 时,我得到 none 或 0。由于数据复制,我手动设置了 id 字段。不确定我错过了什么。
query='database.insert_data'
params ='Josh'
last_id = mysql_conn.insert_proc(query, params)
print(last_id.fetchone())
prints none
class to insert data
def insert_proc(self,query,params):
self.curr.callproc(query, params)
self.conn.commit()
return self.curr
stored procedure
CREATE DEFINER=`me`@`localhost` PROCEDURE `insert_data`(v_name varchar(10))
SET @max_id = (select max(id)+1 from table);
insert into table (id,name)values(@max_id,v_name);
select @max_id;
我对 Python 不是很熟悉,但根据 MySQL 文档 here:
Result sets produced by the stored procedure are automatically fetched
and stored as MySQLCursorBuffered instances. For more information
about using these result sets, see stored_results().
"see stored_results()" 导致代码示例在 cursor.stored_results()
.
上进行迭代
我正在通过存储过程插入数据,并试图获取最后插入的 ID。正在插入数据。但是当我尝试 return 最后一个 ID 时,我得到 none 或 0。由于数据复制,我手动设置了 id 字段。不确定我错过了什么。
query='database.insert_data'
params ='Josh'
last_id = mysql_conn.insert_proc(query, params)
print(last_id.fetchone())
prints none
class to insert data
def insert_proc(self,query,params):
self.curr.callproc(query, params)
self.conn.commit()
return self.curr
stored procedure
CREATE DEFINER=`me`@`localhost` PROCEDURE `insert_data`(v_name varchar(10))
SET @max_id = (select max(id)+1 from table);
insert into table (id,name)values(@max_id,v_name);
select @max_id;
我对 Python 不是很熟悉,但根据 MySQL 文档 here:
Result sets produced by the stored procedure are automatically fetched and stored as MySQLCursorBuffered instances. For more information about using these result sets, see stored_results().
"see stored_results()" 导致代码示例在 cursor.stored_results()
.