One minor point of style/performance is that it is often nice
to use placeholders.
my $sth = $dbh->prepare( 'SELECT Questions, Answers from QA where ID =
+ ?' );
$sth->execute($id);
# put your bind_columns and fetches in
# and if you really want then you can do
$sth->finish;
$sth->execute($id2);
# and get another set of data (thought this situation doesn't look lik
+e it wants that)
This technique is know as using placeholders and has several advantages to your technique of interpolation in the SQL statement.
- Statements prepared are cached for performance, which is no use if you keep on changing them slightly
- If data you are interpolating contains characters such as " or ' they will break the statement. To solve this you can run all the data through $dbh->quote(), but placeholders are easier than that.
- Placeholders are cool :)
- They allow easy SQL statement reuse if you are applying a query multiple times with only the parameters tested in the WHERE clause are changing.