How can I connect to Sybase with Perl? - sql

I'm trying to a good Perl module to use for connecting to a Sybase database.
My Googling has led me to see sybperl as a possible choice, but it hasn't been updated since 2005.

Use DBD::Sybase (via DBI). I use this regularly with FreeTDS to connect to SQL Server, but it is actually written against the CT-Lib interface for Sybase.

I found DBD::Sybase the best module to use for Sysbase DB and Sybase::DBlib - Sybase DB-Library API.

Here is how you connect using perl with ODBC to sybase on linux(64 bit)-
Install Sybase Open Client and ODBC driver. (You can get these two from ASE SDK or from ASE server installation)
You need a odbc driver manager, i have tried with unixODBC
Define a driver config like -
[Adaptive Server Enterprise]
Description = Sybase ODBC Driver
Driver = /sybase/DataAccess64/ODBC/lib/libsybdrvodb.so
FileUsage = -1
Here is a perl sample -
#!/usr/bin/perl
use strict;
use DBI;
use DBD::ODBC;
BEGIN {
$ENV{SYBASE} = "/sybase";
}
my $user = "";
my $passwd = "";
my $server = "";
my $database = "pubs1";
my $port = "5000";
my $data_source = "DBI:ODBC:DRIVER={Adaptive Server Enterprise};server=$server;port=$port;database=$database;";
my #drivers = DBI->available_drivers;
print join(", ", #drivers), "\n";
my $dbh = DBI->connect($data_source, $user, $passwd)
or die "Can't connect to $data_source: $DBI::errstr";
my $statement = "SELECT * FROM <table_name>";
my #row = $dbh->selectrow_array($statement);
print "#row\n";
$dbh->disconnect;
For detailed steps, see - http://kapilraju.tumblr.com/post/131288341356/connect-to-sybase-using-perl-odbc

Related

Connecting to SQL Server using Perl using DBI?

I am trying to connect to SQL server using Perl DBI module, I have tried all the connection string format still Perl is throwing invalid connection string error.
I have already tried code snippet available on perl monk website.
#!/usr/bin/perl -w
use strict;
use DBI;
# Set up variables for the connection
my $server_name = '00.120.124.1;3181';
my $database_name = 'abcd';
my $database_user = 'kkkk';
my $database_pass = 'password';
my $DSN = 'driver={SQL Server};server=$server_name;da
+tabase=$database_name;uid=$database_user;pwd=$database_pass;';
my $dbh = DBI->connect("DBI:ODBC:$DSN") || die "Couldn't open database
+: $DBI::errstr\n";
Expected result is to connect to Database.
failed: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (SQL-01000) [state was 01000 now 01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (SQL-01S00) at perl.pl line 16. Couldn't open database +: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001) [state was 08001 now 01000] Invalid connection string attribute (SQL-01S00)
The lines of your post that begin with +
+tabase=$database_name;uid=$database_user;pwd=$database_pass;';
+: $DBI::errstr\n";
were incorrectly copied and pasted from perlmonks.org. The leading + sign is a convention that indicates a long line was split. You should delete the + and join the line to the previous line, so that your code will read
my $DSN = 'driver={SQL Server};server=$server_name;database=$database_name;uid=$database_user;pwd=$database_pass;';
my $dbh = DBI->connect("DBI:ODBC:$DSN") || die "Couldn't open database: $DBI::errstr\n";
Your error message contains the following:
SQL Server does not exist or access denied
Your set-up code contains the following:
my $server_name = '00.120.124.1;3181';
'00.120.124.1;3181' is not a valid server name or IP address. You should correct the IP address section (it can't start with '00') and remove the port into a separate parameter.
You also have the username and password as part of your DSN. I don't know if DBD::ODBC supports that usage (it's not mentioned in the documentation) but it's more traditional to have those as separate parameters to the connect() call.
All in all, I think you want something more like this:
my $server_name = '00.120.124.1'; # But this needs to be corrected
my $server_port = 3181;
my $database_name = 'abcd';
my $database_user = 'kkkk';
my $database_pass = 'password';
my $DSN = "driver={SQL Server};server=$server_name;port=$server_port;database=$database_name";
my $dbh = DBI->connect("DBI:ODBC:$DSN", $database_user, $database_pass)
|| die "Couldn't open database: $DBI::errstr\n";
Also note that I've changed the quote characters used to create your $DSN variable from single quotes to double quotes. Variables are not expanded in signel quotes, so you weren't getting the values of $server_name, etc. in your DSN.
The better way to connect is with the DBD::Sybase module. The name wouldn't suggest it but SQL Server takes its lineage from Sybase. That way you can avoid ODBC. http://metacpan.org/pod/DBD::Sybase#Using-DBD::Sybase-with-MS-SQL

Inspec test cases for windows authentication

I am trying to validate (mssql) Sql authentication mode "Integrated" using inspec. I was not able find any reference. How to pass sql query using ruby as i am having sql query which displays the current sql authentication mode.
You can check the authentication mode with powershell, see this article for a reference on the powershell code and this article on how the Microsoft.SqlServer.Management.Smo assembly is loaded:
# Connect to the instance using SMO $s = new-object
('Microsoft.SqlServer.Management.Smo.Server') 'MyServer\MyInstance'
[string]$nm = $s.Name [string]$mode = $s.Settings.LoginMode
write-output "Instance Name: $nm"
write-output "Login Mode: $mode"
Therefore you can write a powershell test to achieve what you want:
control 'sql auth type' do
impact 1.0
ps_script = <<-EOH
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $env:COMPUTERNAME
[string]$nm = $s.Name
[string]$mode = $s.Settings.LoginMode
write-output $mode
EOH
describe powershell(ps_script) do
its('stdout') { should match (/Integrated/)}
end
end

how to invoke SQL inside perl script

I am trying to connect with database and perform some SQL queries by using this code, but every time it hangs.
my $connect_str = `/osp/local/etc/.oralgn $srv_name PSMF`;
my $sqlFile = "/osp/local/home/linus/amit/mytest.sql";
my ($abc, $cde)= split (/\#/ , $connect_str );
print "$abc";
$ORACLE_SID=SDDG00;
`export $ORACLE_SID`;
#chomp($abc);
#$abc=~ s/\s+$//;
`sqlplus $abc`;
open (SQL, "$sqlFile");
while (my $sqlStatement = <SQL>) {
$sth = dbi->prepare($sqlStatement)
or die (qq(Can't prepare $sqlStatement));
$sth->execute()
or die qq(Can't execute $sqlStatement);
}
How do I invoke a SQL command inside Perl?
Reading the documentation for the DBI module would be a good start.
Your problem seems to be this line.
$sth = dbi->prepare($sqlStatement)
You're trying to call the prepare method on the class "dbi". But you don't have a class called "dbi" in your program (or, at least, I can't see one in the code you've shown us).
To use a database from Perl you need to do these things:
1/ Load the DBI module (note, "DBI", not "dbi" - Perl is case sensitive).
use DBI;
2/ Connect to the database and get a database handle (Read the DBD::Oracle documentation for more details on the arguments to the connect() method).
my $dbh = DBI->connect('dbi:Oracle:dbname', $user, $password);
3/ You can then use this database handle to prepare SQL statements.
my $sth = $dbh->prepare($sqlStatement);

Can connect with PERL but can't query MSSQL 2k8 database

I am able to connect to my MSSQL server but getting a permission error when trying
to query the database. I see the "YAY" in the script below, but get this error:
*[Microsoft][ODBC SQL Server Driver][SQL Server]The SELECT permission was denied on the object 'TICKET_TYPES', database 'DBNAME', schema 'dbo'. (SQL-42000)(DBD: st_execute/SQLExecute err=-1)*
If I add "DOMAIN\DOMAIN Users" with DBO access it works, but when I take that away it doesn't. I think it's connecting to the database as the logon user, not the SQL user that is set up in the connect string but not positive.
The SQL Driver is set to use "With SQL Server Authentication using a login ID..." and not "Windows NT..."
Classic ASP connects and queries fine. I can access/query the DB using the SQL account in the connect string via ASP classic and SQL Server Management Studio.
It may be relevant that it also works perfectly against SQL2005 but not SQL2008 which we are trying to upgrade to. Everything is fine with our upgrade except our couple Perl scripts just don't get into the tables.
use lib "cgi-bin";
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
require "Babel.pm";
my $y = new Babel;
my $q = new CGI;
$SERVER_N = $q->server_name() ;
### CONNECT
$database = "DBNAME";
$dbusername = "SQLUSERNAME";
$dbpassword = "SQLUSERPASSWORD";
$server="SQLSERVERIP";
print "Content-type: text/html\n\n";
$dbh = DBI-> connect( "DBI:ODBC:driver={SQL Server};
Server=$server;
Database=$database;
UID=$dbusername;
PWD=$dbpassword") || die &DBerror;
if ($DBI::errstr) {print "Error: Connecting to the Database!";
exit;}else{
print "YAY! <br><br>";
}
### END: CONNECT
$v2SQL = "select distinct ROUTE from TICKET_TYPES";
$css1 = $dbh->prepare ($v2SQL);
$css1->execute () || print $css1->errstr;
while ((#row) = $css1 ->fetchrow_array) {
$MANA = #row[0];
print "$MANA";
}
$css1->finish () || print $css1->errstr;

connecting to sybase DB in perl without DBI

I have tried the following script to connect to the DB.
This way works in oracle,but i am not sure why it is not working for Sybase.
#!/usr/bin/perl
use strict;
use warnings;
my $result = qx { isql -Uxx -Pxxxxxxx -Dxxxxx <<EOF
select count(*) from XXX;
exit;
EOF
};
print "result is :";
print $result;
print "\nbye bye\n";
I tried to connect to sybase DB without DBI.
Please dont tell me to use DBI.even i have know that we can use DBI for this.but unfortunately DBI is not installed here in my server and i am not the admin where i have a authorization to install modules for perl.what ever given to me i have to fully use it. but thats an off topic here.
I repeat the question:
how to connect to sybase DB from perl without using DBI?
The output of the above script is :
> temp.pl
result is :
bye bye
when i manually execute the same thing:
> isql -Uxx -Pxxxxxxxx -Dxxxxx
1> select count(*) from XXX
2> go
-----------
26
(1 row affected)
1> exit
>
I got the solution:
it is because of the semicolon and the go statement.I modified the script as below and its working now.
#!/usr/bin/perl
use strict;
use warnings;
my $result = qx { isql -Uxx -Pxxxxxxx -Dxxxx <<EOF
set nocount on
select count(*) from XXX
go
exit
EOF
};
print "result is :";
print $result;
print "\nbye bye\n";