in reply to SQLite: INSERT into a unique column and retrieve rowid
Which works.sub insert_unique( $table, $column, $value ) { state %sths; if ( !exists $sths{$table} ) { $sths{$table}{SELECT} = $dbh->prepare("SELECT id FROM $table WHERE $column = (?)") +; $sths{$table}{INSERT} = $dbh->prepare("INSERT INTO $table ($column) VALUES (?) RET +URNING id"); } my $rowid; my $sth = $sths{$table}{SELECT}; $sth->execute($value); my $rows = $sth->fetchall_arrayref( {} ); if ($rows->@*) { $rowid = $rows->[0]->{id}; } else { $sth = $sths{$table}{INSERT}; $sth->execute($value); $rows = $sth->fetchall_arrayref( {} ); $rowid = $rows->[0]->{id}; } return $rowid; }
It does seem to me a lot of code to accomplish a seemingly basic requirement. I'm implementing UNIQUEness, for one thing. But it works, and it's pretty fast adding 60,000 songs. (I didn't notice much difference whether or not the column was indexed.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: SQLite: INSERT into a unique column and retrieve rowid
by hippo (Archbishop) on May 04, 2024 at 22:46 UTC | |
by ibm1620 (Hermit) on May 05, 2024 at 20:42 UTC | |
by hippo (Archbishop) on May 06, 2024 at 10:23 UTC | |
by ibm1620 (Hermit) on May 05, 2024 at 01:47 UTC | |
by hippo (Archbishop) on May 05, 2024 at 10:55 UTC | |
|
Re^2: SQLite: INSERT into a unique column and retrieve rowid
by Marshall (Canon) on May 06, 2024 at 14:08 UTC | |
by ibm1620 (Hermit) on May 06, 2024 at 15:10 UTC | |
by Marshall (Canon) on May 06, 2024 at 17:05 UTC |