如何使用Perl检索临时表的SQL字段名?

时间:2021-06-12 15:56:23

Below is the code I'm using to run the query, parse the result set, and parse the rows (respectively)

下面是我用来运行查询、解析结果集和解析行(分别)的代码

$exec_ret = $DBS->SQLExecSQL($STMT);

while ($DBS->SQLFetch() == *PLibdata::RET_OK)
{
      $rowfetch = $DBS->{Row}->GetCharValue($colname[$i]);
}

Can I grab the column/field name of a temp table using similar syntax? $colname[$i] is predefined at the top to hold the column/field names. This is hard-coded right now, but I would rather automate it by pushing values into $colname inside of a loop that runs before the rows are parsed.

我可以使用类似的语法获取临时表的列/字段名吗?$colname[$i]在顶部预定义以保存列/字段名。这是硬编码的,但是我宁愿通过在解析行之前运行的循环中将值放入$colname中来实现自动化。

2 个解决方案

#1


6  

What module are you using for database access? I don't recognize the method names.

您使用什么模块进行数据库访问?我不认识方法名。

If you're using DBI, you can get the column names from the statement handle after executing it:

如果使用DBI,可以在执行后从语句句柄获取列名:

my $sth = $dbh->prepare($STMT);
$sth->execute;
my $columns = $sth->{NAME_uc};

while (my $row = $sth->fetch) {
  for my $i (0 .. $#$row) {
    print "$columns->[$i]: $row->[$i]\n";
  }
  print "\n";
}

There are 3 versions of the column names: NAME gives the column names as the database returns them, NAME_lc converts them to all lower case, and NAME_uc converts them to all upper case. If you care about database independence, I suggest you avoid NAME and use one of the other two.

列名有3个版本:名称赋予列名作为数据库返回它们,NAME_lc将它们转换为所有小写字母,NAME_uc将它们转换为所有大写。如果您关心数据库独立性,我建议您避免使用名称,并使用其他两个名称中的一个。

#2


0  

Try running SHOW TABLE yourtable and treating it as if it were a SELECT.

试着运行SHOW TABLE yourtable,并把它当作一个SELECT。

#1


6  

What module are you using for database access? I don't recognize the method names.

您使用什么模块进行数据库访问?我不认识方法名。

If you're using DBI, you can get the column names from the statement handle after executing it:

如果使用DBI,可以在执行后从语句句柄获取列名:

my $sth = $dbh->prepare($STMT);
$sth->execute;
my $columns = $sth->{NAME_uc};

while (my $row = $sth->fetch) {
  for my $i (0 .. $#$row) {
    print "$columns->[$i]: $row->[$i]\n";
  }
  print "\n";
}

There are 3 versions of the column names: NAME gives the column names as the database returns them, NAME_lc converts them to all lower case, and NAME_uc converts them to all upper case. If you care about database independence, I suggest you avoid NAME and use one of the other two.

列名有3个版本:名称赋予列名作为数据库返回它们,NAME_lc将它们转换为所有小写字母,NAME_uc将它们转换为所有大写。如果您关心数据库独立性,我建议您避免使用名称,并使用其他两个名称中的一个。

#2


0  

Try running SHOW TABLE yourtable and treating it as if it were a SELECT.

试着运行SHOW TABLE yourtable,并把它当作一个SELECT。