Stephen Toney has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I am having trouble with a simple indexing routine which takes data from an ODBC table and creates an index in a SQLite database. The ODBC and SQLite databases use separate connections, dbh's and sth's.

The routine retrieves all data for a column of the ODBC table, indexes it, and then goes to the next column. It works perfectly on the first three columns of the ODBC table, but then fails on the 4th column with this error:

DBD::SQLite::db do failed: no such table: ISSUE(1) at dbdimp.c line 269

However, the table ISSUE is the ODBC table being indexed. So why was the $sth trying to access this table using the SQLite connection?

Here's the code, with error-handling stuff removed for clarity.
use DBI; # connection to ODBC database $::dbh = DBI->connect("dbi:ODBC:$::dsn"); # second connection for SQLite control database $datpath = "d:/mwa/uni/spt/mwebxml.dat"; $::cdbh = DBI->connect("dbi:SQLite:$datpath"); my $query = "select f.tablename, f.fieldname, " . " t.primarykey" . " from mwebfield f, mwebtable t" . " where f.tablename=t.tablename"; # control sth uses control dbh (SQLite) my $csth = $::cdbh->prepare($query); $csth->execute; my ($tablename, $fieldname, $primarykey); $csth->bind_columns(\$tablename, \$fieldname, \$primarykey); while ($csth->fetch) { $query = "select $primarykey, $fieldname from $tablename"; # this sth uses the ODBC $dbh for the first three columns, but # tries to use the SQLite $cdbh for the 4th column my $sth = $::dbh->prepare($query); $sth->execute; my ($key, $value); $sth->bind_columns(\$key, \$value); while ($sth->fetch) { next if !$key; next if !$value; $value = lc($value); my @words = split(/\W/, $value); for (my $i = 0; $i < scalar(@words); $i++) { my $word = $words[$i - 1]; next if !$word; # trim $word =~ s|^ *(.+) *$|$1|; next if !$word; $word = $::cdbh->quote($word); $query = "insert into keyword (zkey, value)" . " values ('$tablename.$key', $word)"; $::cdbh->do($query); } }

Replies are listed 'Best First'.
Re: DBI using wrong connection
by holli (Abbot) on Sep 05, 2006 at 13:46 UTC
    You are preparing two different queries within loops. This can be tricky because some databases don't release the cursor of the query, thus running out of cursors after a while (been there and seen it).
    While the loop is inevitable for the first query, you can and should put the second (update) query outside the loop:
    my $insert_query = $cdbh->prepare("insert into keyword (zkey, value) v +alues (?, ?)"; while ($csth->fetch) { $query = "select $primarykey, $fieldname from $tablename"; # this sth uses the ODBC $dbh for the first three columns, but # tries to use the SQLite $cdbh for the 4th column my $sth = $::dbh->prepare($query); $sth->execute; my ($key, $value); $sth->bind_columns(\$key, \$value); while ($sth->fetch) { next if !$key; next if !$value; $value = lc($value); my @words = split(/\W/, $value); for (my $i = 0; $i < scalar(@words); $i++) { my $word = $words[$i - 1]; next if !$word; # trim $word =~ s|^ *(.+) *$|$1|; next if !$word; $insert_query->execute("$tablename.$key", $word)";); } } }
    Note that using $dbh->quote is not neccessary when you use placeholders. Also note that this is untested.


    holli, /regexed monk/
      Holli: Many thanks for the clear reply. You were right about the problem and the solution! Best regards.