I am inserting data via a store procedure and I am trying to get the last id inserted. Data is being inserted. But when I try to return the last ID I get none or 0. I set the id field manually because of data replication. Not sure what I am missing.
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;
答案 0 :(得分:1)
我对Python不是很熟悉,但根据MySQL文档here:
自动获取存储过程生成的结果集 并存储为MySQLCursorBuffered实例。欲获得更多信息 关于使用这些结果集,请参阅stored_results()。
“see stored_results()”会导致代码示例以cursor.stored_results()
上的迭代为特色。