import uuid
batch_insert_id = uuid.uuid4().hex
# Execute your batch insert query, with batch_insert_id
# as value of batch_insert_id for all rows
cursor.execute("SELECT id FROM mytable WHERE batch_insert_id = '%s'", (batch_insert_id,))
primary_keys = [row[0] for row in cursor.fetchall()]
MySQL的
LAST_INSERT_ID()
只返回一个值。如果是批量插入,则返回第一个插入行的主键值。在您可以使用GUID标记来跟踪作为批插入操作一部分的行。向表中添加一个额外的
varchar(32)
列,名为batch_insert_id
。执行批插入时,请使用已知的GUID值。插入后,执行select查询以获取插入行的主键。在相关问题 更多 >
编程相关推荐