如何從Perl腳本對SQL Server 2005執行SELECT操作?從Perl連接到SQL Server 2005並執行SELECT
2
A
回答
4
您將需要使用DBI,你可能是最好的使用來自(CPAN)的DBD :: ODBC提供程序。如果您不瞭解DBI,那麼您需要閱讀相關內容。有一本書(Programming the Perl DBI)這是舊的,但仍然有效。
然後像下面這樣:
use strict;
use warnings;
use DBI;
# Insert your DSN's name here.
my $dsn = 'DSN NAME HERE'
# Change username and password to something more meaningful
my $dbh = DBI->connect("DBI:ODBC:$dsn", 'username', 'password')
# Prepare your sql statement (perldoc DBI for much more info).
my $sth = $dbh->prepare('select id, name from mytable');
# Execute the statement.
if ($sth->execute)
{
# This will keep returning until you run out of rows.
while (my $row = $sth->fetchrow_hashref)
{
print "ID = $row->{id}, Name = $row->{name}\n";
}
}
# Done. Close the connection.
$dbh->disconnect;
2
下面是使用DBI一個基本的例子(評論後可進行編輯):
use DBI;
my $dbh = DBI->connect("dbi:Sybase:database=<dbname>;server=<servername>",
<user>, <password>,
{ PrintError => 0, RaiseError => 1 });
my $sth = $dbh->prepare("select field from table");
my $result = $sth->execute();
while(my $result = $sth->fetchrow_hashref) {
print $result->{field};
}
$sth->finish;
$dbh->disconnect;
希望能看到其他的答案有一個簡單的解決方案:)
0
#
# ------------------------------------------------------
# run a passed sql and retun a hash ref of hash refs
# ------------------------------------------------------
sub doRunSqlGetHashRef {
my $self = shift ;
my $sql = shift ;
my $hsr_meta = {} ;
my $hsr = {} ;
my $rowid = 0 ;
my $flag_filled_hsr_meta = 0 ;
my $hsr_meta_colid = 0 ;
use DBI;
my $dbs = "dbi:ODBC:DRIVER=FreeTDS;DSN=DEV_MSSQLSRV_DSN";
# verify by :
# isql -v DEV_MSSQLSRV_DSN user pwd
my $dbh = DBI->connect($dbs, $db_user, $db_user_pw)
or die "CONNECT ERROR! :: $DBI::err $DBI::errstr $DBI::state $!\n";
if (defined($dbh)) {
# Prepare your sql statement (perldoc DBI for much more info).
my $sth = $dbh->prepare($sql) ;
# Execute the statement.
if ($sth->execute) {
# This will keep returning until you run out of rows.
while (my $row = $sth->fetchrow_hashref) {
# fill in the meta hash reference with the col names
if ($flag_filled_hsr_meta == 0) {
for (@{$sth->{ 'NAME' }}) {
# debug ok print "$_ => $row->{$_}\t";
$hsr_meta->{ $hsr_meta_colid } = $_ ;
$hsr_meta_colid++ ;
$flag_filled_hsr_meta = 1 ;
}
}
# p ($row) ; # row level debug ...
$hsr->{ $rowid } = $row ;
$rowid++ ;
}
}
# Done. Close the connection.
$dbh->disconnect;
# debug ok p($hsr_meta) ;
return ($hsr_meta , $hsr) ;
}
else {
print "Error connecting to database: Error $DBI::err - $DBI::errstr\n";
}
}
#eof sub doRunSqlGetHashRef
相關問題
- 1. 連接到SQL Server 2005
- 2. Ruby連接到SQL Server 2005
- 3. 從Visual Studio到SQL Server 2005的連接
- 4. Perl ssh連接並執行telnet連接
- 5. 從SQL Server 2005 SP3到SQL Server 2012 SP1的連接字符串
- 6. SQL Server 2005與MySQL連接
- 7. C#和SQL Server 2005連接
- 8. SQL Server 2005連接問題
- 9. SQL SERVER 2005連接問題
- 10. SQL Server 2005遠程連接
- 11. 無法連接到SQL Server 2005
- 12. Rails 2.3.5連接到SQL Server 2005
- 13. 錯誤,同時連接到SQL Server 2005
- 14. 無法連接到遠程SQL Server 2005
- 15. BIRT:如何連接到SQL Server 2005
- 16. 將SQL Server 2005連接到Access 2.0
- 17. Access 2000連接到SQL Server 2005
- 18. 想要連接PHP5到MS SQL SERVER 2005
- 19. 從Sql Server 2005執行DTS包
- 20. 如何連接SQL Server與Perl
- 21. sql server select與like連接?
- 22. 如何使用Perl連接到SQL Server?
- 23. 從SQL Server執行SQL Server連接字符串
- 24. 從access連接到sql server
- 25. 在SQL Server Management Studio Express中連接到SQL Server 2005數據庫
- 26. 鏈接表 - SQL Server 2008到SQL Server 2005
- 27. 當連接到SQL Server 2005時,SQL Server不允許遠程連接
- 28. sqsh SQL Server 2005執行* .sql文件
- 29. 從android連接到sql server
- 30. 連接到SQL Server
你不一般需要指定dbi_connect_method。我建議使用「PrintError => 0,RaiseError => 1」,以便引發SQL錯誤......特別是因爲您不做任何錯誤檢查;) 您甚至可以通過簡單地說「for my $ row in(@ {$ dbh-> selectall_arrayref(「select table from table」,{Slice => {}})}){print「$ row - > {field} \ n」;} 如果你是打算使用fetchrow_hashref或其朋友,值得設置「FetchHashKeyName =>'NAME_lc'」,當連接到DB時,有些人堅持使用CamelCase作爲列名 但除了這一切,罰款:) – araqnid 2009-05-22 13:02:10
好的建議,編輯 – Andomar 2009-05-22 14:36:47