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.)
In reply to Re: SQLite: INSERT into a unique column and retrieve rowid
by ibm1620
in thread SQLite: INSERT into a unique column and retrieve rowid
by ibm1620
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |