Understanding Perl DBI bind_column

Asked 2 years ago, Updated 2 years ago, 111 views

Run a select statement using Perl's DBI and read each line.

$th->bind_columns(\$name, \$old, \$language);
while($th->fetch()) {
...
}

I'm running the code
in the line where $old on the DB does not contain any data. The action in the while statement is omitted and the next line is moved.

What should I do if $old is empty?

perl

2022-09-30 16:31

1 Answers

I was going to leave a comment, but I'll take this one because it contains the actual code.

Some important information is missing from the question.
First, could you give me the executable code and tell me exactly what the problem is?

Below is the code and results from testing with SQLite.$old looks fine even in color:

#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use DBD::SQLite;
use Data::Dumper;


# Database Handlers on Memory
my$dbh=DBI->connect('DBI:SQLite:');

# Creating a test table with only foo,bar,buz
$dbh->do(q{create table test(name text, old text, language text)});

# prepare test data
$dbh->do(q{
    insert into test(name,old,language) values(?,?,?)
}, undef, @{$_})for(
    ['foo1', 'bar1', 'buz1'],
    ['foo2', undef, 'buz2', #deliberately pull out bar2]
    ['foo3', 'bar3', 'buz3',]
);

# SQL to retrieve name,old,language
my$th=$dbh->prepare(q{select name, old, language from test});
$th->execute;

# Link each to $name, $old, $language
my($name,$old,$language);
$th->bind_columns(\$name,\$old,\$language);

while($th->fetch()){
    print Dumper [$name,$old,$language];
}

$dbh->disconnect;
__END__
$VAR1 = [
          'foo1',
          'bar1',
          'buz1'
        ];
$VAR1 = [
          'foo2',
          undef,
          "buz2".
        ];
$VAR1 = [
          'foo3',
          'bar3',
          'buz3'
        ];


2022-09-30 16:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.