I have MySQL database with table into which I insert from multiple files using
LOAD DATA LOCAL INFILE ...
statement. I have PRIMARY KEY ID
set to auto_increment. The problem is, when I want to update only part of the table.
Say I've inserted file_1, file_2, file_3
in the past and now I want to update only file_2
. I imagine the process in pseudo workflow
- delete old data related to
file_2
- insert new data from
file_2
However, it is hard to determine, which data are originally from file_2
. In order to find out, I've come up with this idea:
When I insert the data, I will note the ids of the rows, which I've inserted, since I am using auto_increment I can note something like from_id, to_id
for each of the file. Then, when I want to update only file_x
I will delete only the data with from_id <= id <= to_id
(where from_id, to_id
relates to the file_x
).
After little bit of searching, I've found out about @@identity
and last_insert_id()
(see), however, when I use select last_insert_id()
after LOAD DATA LOCAL INFILE
I get only one id, and not the maximal id corresponding to the data, but the last added (as it is defined). I am connecting to the database from Python using mysql.connnector
using
cur.execute("select last_insert_id();")
print(cur.fetchall())
# gives
# [(<some_number>,)]
So, is there a way, how to retrieve all (or at least the minimal and maximal) ids which were assigned to the data imported using the LOAD DATA LOCAL INFILE...
statement as mentioned above?