简体   繁体   English

如何使用DBD :: ODBC指定端口号?

[英]How can I specify the port number with DBD::ODBC?

I currently use the following code to connect to a database in my Perl script: 我目前在Perl脚本中使用以下代码连接到数据库:

my $dsn = 'dbi:ODBC:MYDATABASE';
my $database = 'uat_env';
my $user = 'user';
my $auth = 'password';

my $dbh = DBI->connect($dsn, $user, $auth, {
    RaiseError => 1,
    AutoCommit => 1
}) or die("Couldn't connect to database");

$dbh->do('use '.$database);

Now the port has changed from 1433 to 40450. 现在,端口已从1433更改为40450。

I am having trouble changing the port in the DSN. 我在更改DSN中的端口时遇到问题。 I thought this change would work but I am receiving a "DSN not found" error: 我以为此更改会起作用,但是我收到“找不到DSN”错误:

my $dsn = 'dbi:ODBC:MYDATABASE;Port=40450';

Any idea why this isn't working? 知道为什么这行不通吗?

There are two formats for a DBI data source string for ODBC. ODBC的DBI数据源字符串有两种格式。 You can say either 你可以说

dbi:ODBC:DSN=MYDATABASE

or you can abbreviate that to 或者您可以将其缩写为

dbi:ODBC:MYDATABASE

which is what you have. 这就是你所拥有的。 If you use just the DSN then you can't add any more parameters, so your dbi:ODBC:MYDATABASE;Port=40450 is looking for DSN MYDATABASE;Port=40450 which clearly doesn't exist 如果使用DSN,则无法再添加任何参数,因此dbi:ODBC:MYDATABASE;Port=40450正在查找DSN MYDATABASE;Port=40450 ,这显然不存在

The proper way to do this is to set up a new DSN which has a copy of all the parameters of MYDATABASE , but with a different port name 正确的方法是设置一个新的DSN,该DSN包含MYDATABASE所有参数的副本,但端口名称不同

At a guess , I would say you may be able to write 大概 ,你也许会写

dbi:ODBC:DSN=MYDATABASE;Port=40450

but I can't be sure and I have no way of testing 但我不确定,我无法测试

If your requirements are simple then you can supply all of the parameters instead of a DSN, like this 如果您的要求很简单,那么您可以提供所有参数而不是DSN,就像这样

dbi:ODBC:Driver={SQL Server};Server=11.22.33.44;Port=40450

but you will have to supply the correct driver if you aren't using a SQL Server ODBC connection, and other parameters may be necessary 但是如果您不使用SQL Server ODBC连接,则必须提供正确的驱动程序,并且可能需要其他参数

You should start by examining the values in the MYDATABASE DSN and go from there 您应该首先检查MYDATABASE DSN中的值,然后从那里开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何指定用于安装DBD :: mysql的自定义MySQL套接字位置? - How can I specify a custom MySQL socket location for installing DBD::mysql? 如何使用Perl和DBD :: ODBC从SQL Server查询视图? - How do I query a view from SQL server with Perl and DBD::ODBC? 如何让DBD :: Pg可靠地超时? - How can I get DBD::Pg to time out reliably? 如何告诉DBD :: CSV使用逗号作为小数分隔符? - How can I tell DBD::CSV to use a comma as the decimal seperator? 如何将Perl中的2个CSV文件与DBI和DBD :: CSV合并? - How can I merge 2 CSV files in Perl with DBI and DBD::CSV? 如何检查Perl模块(DBD :: mysql)是否正确安装? - How can I check if a Perl module (DBD::mysql) is properly installed? 如果未安装postgres,如何安装DBD :: Pg? - How can I install DBD::Pg if postgres is not installed? install_driver(ODBC)失败:无法为模块DBD :: ODBC:libodbc.so.1加载'/usr/local/lib64/perl5/auto/DBD/ODBC/ODBC.so' - install_driver(ODBC) failed: Can't load '/usr/local/lib64/perl5/auto/DBD/ODBC/ODBC.so' for module DBD::ODBC: libodbc.so.1 ERR:install_driver(ODBC)失败:在@INC中找不到DBD / ODBC.pm - ERR: install_driver(ODBC) failed: Can't locate DBD/ODBC.pm in @INC 无法为模块DBD :: ODBC:libodbc.so.1加载'/usr/local/lib64/perl5/auto/DBD/ODBC/ODBC.so' - Can't load '/usr/local/lib64/perl5/auto/DBD/ODBC/ODBC.so' for module DBD::ODBC: libodbc.so.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM