Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Perl DBI and Foreign Keys

by Marshall (Canon)
on Apr 08, 2019 at 10:05 UTC ( [id://1232286] : note . print w/replies, xml ) Need Help??


in reply to Re: Perl DBI and Foreign Keys
in thread Perl DBI and Foreign Keys

Thanks!
I wanted to hear from somebody who had used this function.
My target DB's are MySQL and SQLite. If I have code that works with both of those DB's, I'm fine.
It appears that last_insert_id will work with both of these DB's.

I did consider the write, then read back to get the auto id, but that is rather goofy sounding and may not be possible in certain situations.

I'm starting a new project using SQlite. The DB access is just a tiny part, but an important part.

Replies are listed 'Best First'.
Re^3: Perl DBI and Foreign Keys
by hdb (Monsignor) on Apr 08, 2019 at 12:30 UTC

    And if you do not like last_insert_id, you can always build an index generator yourself, like max( index_column ) + 1.

      I don't think that would work as a substitute for last_insert_id due to race conditions with multiple writers.

      I can read a table. Calculate the number of rows. And wind up with a different number of rows than you read doing the same thing if somebody else inserted a row during this process.

        what if it's wrapped within a transaction?
Re^3: Perl DBI and Foreign Keys
by dsheroh (Monsignor) on Apr 09, 2019 at 07:29 UTC
    I wanted to hear from somebody who had used this function. My target DB's are MySQL and SQLite.
    In that case, since it wasn't explicitly stated before: I have personally used last_insert_id with both MySQL and SQLite.
Re^3: Perl DBI and Foreign Keys
by afoken (Chancellor) on Apr 09, 2019 at 18:37 UTC