我正在使用DBI来查询sqlite3数据库.我有什么工作,但它不返回列按顺序.例:
Query: select col1,col2,col3,col4 from some_view; Output: col3,col1,col4 3,2,1,4 3,4 ... (values and columns are just for illustration)
我知道这是因为我使用哈希,但是如果我只使用一个数组,我还能如何得到列名?所有我想做的是获得这样的任何任意查询:
col1,col4 1,3,4 1,4 ...
(也就是说,我需要的输出是正确的顺序和列名.)
我非常喜欢Perl新手,但我真的以为这将是一个简单的问题. (我以前在Ruby和PHP中完成了这些工作,但是我无法在Perl文档中跟踪我正在寻找的内容.)
这是我现在所做的一个削减版本:
use Data::Dumper; use DBI; my $database_path = '~/path/to/db.sqlite3'; $database = DBI->connect( "dbi:sqlite:dbname=$database_path","",{ RaiseError => 1,AutoCommit => 0,} ) or die "Couldn't connect to database: " . DBI->errstr; my $result = $database->prepare('select col1,col4 from some_view;') or die "Couldn't prepare query: " . $database->errstr; $result->execute or die "Couldn't execute query: " . $result->errstr; ########################################################################################### # What goes here to print the fields that I requested in the query? # It can be totally arbitrary or '*' -- "col1,col4" is just for illustration. # I would expect it to be called something like $result->fields ########################################################################################### while (my $row = $result->fetchrow_hashref) { my $csv = join(',',values %$row); print "$csv\n"; } $result->finish; $database->disconnect;