I have some objects that will be created an entered into the database. There is no guarantee that any of their fields will be unique except for the auto-incrementing primary key. I would like to be able to access these objects later in the program using their primary key.
What is the best way to get this key out of the database? I can think of two methods to obtain the primary key after adding the object's data to the database:
- Find the maximum primary key as this was the one most recently added.
- Delete the current object, compare the data in the database with the data in the program, and then create a new object based on the row that is not contained in the program.
Both of these seem error-prone and hacky and I am wondering if there was a more standard way do to this.