Not able to connect to .accdb file using perl - perl

I am trying to connect to access 2007 database using perl. Below is the code that I tried executing
use DBI;
$path='T:\a\abc.accdb';
$datasource = "driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=$path";
$dbh = DBI->connect("dbi:ODBC:$datasource", '', '') || die "Error connecting: $!";
$query="select name from receiver";
$exe=$dbh->prepare($query) or die "cannot prepare";
$exe->execute or die "cannot execute";
$a=$exe->fetchall_arrayref();
foreach $aa(#{$a})
{
print #$aa;
}
but the execution fails with the below error
DBI connect('driver={Microsoft Access Driver(*.mdb,*.accdb)};DBQ=T:\a\abc.accdb','',...)failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at test.pl line 5.
Error connecting: at test.pl line 5.
can someone please guide me how to resolve this issue.

Your driver name is missing a space. ODBC is very fussy about that. Try
$datasource = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=$path";
Also, NAME is a reserved word in Access SQL so you may need to use
$query="select [name] from receiver";

My experience with DBI:ODBC is somewhat dated but I would suggest the following:
Check the driver names in the ODBC datasource setup and make sure
that you have the spelling right. (spaces and all) - Maybe set up a
DSN and use that instead - more reliable
Check that the file exists (I don't think that is the problem from
the error message)
Capitalization on the Driver/Dbq (not likely but we are dealing with
MS)

Related

Informix error in Perl: ISAM: 107: ISAM error: record is locked. at test_db_connectivity.pl line 14

I'm trying to connect to our database in our production environment. I am getting the 107 ISAM error: record is locked . I have checked various articles on what it could be, but no luck. I double checked the database details with our DBA and the information is correct.
FYI - Also the first error previous to the "record is locked" error I received was SQL: -931: Cannot locate web1_tcp service/tcp service in /etc/services. at test_db_connectivity.pl line 13. . I worked with our SA to get that entered and that is no longer appearing. Can anyone offer any guidance on the ISAM 107 error? Is my connection string wrong? There shouldn't be a lock because I am not even opening a connection to the database.
test_db_connectivity.pl:
#/usr/bin/perl -w
use DBI;
use strict;
use DBI qw(:sql_types);
use DBD::Informix qw(:ix_types);
my $driver = "Informix";
my $database = "web1";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "user";
my $password = "password";
print "I got this far!\n";
my $dbh = DBI->connect($dsn, $userid, $password, {RaiseError => 1}) or die $DBI::errstr;
print "Opened db successfully\n";
$dbh->disconnect();
Some of the variables set:
export INFORMIXDIR=/opt/informix
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INFORMIXDIR/lib:$INFORMIXDIR/lib/cli:$INFORMIXDIR/lib/esql:$INFORMIXDIR/lib/tools
export INFORMIXSERVER=web1
INFORMIX SQLHOSTS FILE:
#dbservername nettype hostname servicename options
web1 onsoctcp 10.199.***.* web1dev1_tcp
etc/services FILE:
web1dev1_tcp 1533/tcp # Port assigned to database
virtual-places 1533/tcp # Virtual Places Software
web1dev1_tcp 1533/udp # Port assigned to database
On Linux, there is a system error 107:
107 (ENOTCONN): Transport endpoint is not connected
It is hard to distinguish that from a C-ISAM error 107 (the number is the same). However, in context, I think the ENOTCONN is far more likely to be the problem than
ISAM ERROR 107: record is locked.
I observe that you're using a notation in $dsn that DBD::Informix doesn't test for:
my $dsn = "DBI:$driver:dbname=$database";
The dbname= portion of that is incorrect. You should use:
my $dsn = "DBI:$driver:$database";
Note that the DBI spec (when I last looked) said that what follows the driver name is driver-specific. You could submit an enhancement request.
However, on my working system, when I use the dbname=stores notation, the error I get is:
DBI connect('dbname=stores', '',...) failed: SQL: -354: Incorrect database or cursor name format. at dbname-dsn.pl line 8.
That's quite different. You say you got:
SQL: -931: Cannot locate web1_tcp service/tcp service in /etc/services. at test_db_connectivity.pl line 13
The error message is quite explicit. You need an entry in /etc/services such as:
web1_tcp 9088/tcp
At the moment, you do not have such an entry, and therefore the connection is not even attempted, much less successful.

Error while connecting to sybase database using dbd:sybase

I have compiled dbd-sybase module(1.07) to add it to my perl(5.12.3) on windows. However, when I trying to use it using following script:
use DBI;
DBD::Sybase;
$ENV{SYBASE} = 'E:\Sybase';
$ENV{SYBASE_OCS} = 'OCS-15_0';
$ENV{SYBASE_ASE} = 'ASE-15_0';
$SERVER="ABC";
$DATABASE="dadw";
$USER_NAME="";
$PASSWORD="";
$dbh = DBI->connect("dbi:Sybase:server=$SERVER;databse=$DATABASE",$USER_NAME, $PASSWORD);
I am getting following error message:
install_driver(Sybase) failed: DBD::Sybase initialize: cs_ctx_alloc(113) failed
at c:/perl/lib/DynaLoader.pm line 223.
Compilation failed in require at (eval 4) line 3.
at b.pl line 11
Can someone help in solving this error message.
It appears that the driver is using CT-Connect libraries, so I would guess it needs to have valid server information in the interfaces file.
On windows the file is usually located in C:/sybase/interfaces.ini
Check this answer for more details on creating interface file entries.
Using Sybase ASE BCP to a Remote Server
In Sybase, the interfaces file in Windows is called sql.ini and it is located in the c:Sybase/ini/ folder.

Error while connecting to Sybase DB

I have a perl script which connects to a Sybase db server (alias for server - MYDATABASESERVER). My code is:
exec perl -w -x
#!perl
use Sybase::DBlib;
use Mail::Sendmail;
use Env qw(DSQUERY DBNAME DBUSER DBPASSWD);
$dbh = &execRemoteSQL($sql_text);
sub execRemoteSQL
{
my ($sqlText) = #_;
my ( $ret, $retS );
local ($dbh) = undef;
$dbh = new Sybase::DBlib $DBUSER, $DBPASSWD, $DSQUERY;
if ( !(defined $dbh) )
{
print STDERR "execRemoteSQL(): Failed To Create DB Handle for :\n";
print STDERR " SERVER = $DSQUERY\n";
print STDERR " DATABSE = $DBNAME\n";
exit(-1);
}
.
.
.
When i provide the server name as MYDATABASESERVER (value of DSQUERY), I get the error in the If statement, but it connects properly to the server with name as MYDB.
Wanted to know if there is any constraint on the server name length or is it due to something else.
Here is the error messsage i am getting:
DB-Library error:
Unknown host machine name.
execRemoteSQL(): Failed To Create DB Handle for :
SERVER = MYDATABASESERVER
DATABSE = my_db
The server alias is defined properly because i am able to connect to the db using isql.
I don't think the length of the server name is too long here.
What is your exact error message?
Maybe the MYDATABASESERVER server-alias name is not defined properly in the Sybase-specific "interfaces" config file. It resides in dir $SYBASE/$SYBASE_OCS. What does it say about MYDATABASESERVER (should be 2 lines)?
In any case:
Don't use DBLib, it is really old and deprecated. I think the maintainer of the Sybperl/DBLib and DBD::Sybase modules, only still supports the Sybase::DBLib code to make life easier for client programmers who have to deal with old programs, and to keep legacy applications compatible with new releases of Sybase ASE.
You should use DBI and DBD::Sybase instead. 99% of all perl/sybase code on the internet uses DBI.
Can you try the following syntax instead? :
$dbh = Sybase::DBlib->new( $DBUSER, $DBPASSWD, $DSQUERY );

How do I connect with Perl to SQL Server?

I have a user id, password, database name and datasource details. I want to connect with Perl to a MSSQL server. I just used the following snippet, but I am getting an error.
#!/usr/bin/perl -w
use strict;
use DBI;
my $data_source = q/dbi:ODBC:192.168.3.137/;
my $user = q/bharani/;
my $password = q/123456/;
# Connect to the data source and get a handle for that connection.
my $dbh = DBI->connect($data_source, $user, $password)
or die "Can't connect to $data_source: $DBI::errstr";
My error is:
DBI connect('192.168.3.137','bharani',...) failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at my sqlconnect.pl line 14
Can't connect to dbi:ODBC:192.168.3.137: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at mysqlconnect.pl line 14.
The SQL server runs on another system, I am just trying to connect with above details. Please tell me, should I crease DSN in my system, or is anything missing in my program?
Everything following 'dbi:ODBC:' in your connection string is passed to the ODBC driver. For MSSQL, try this connection string:
DBI->connect("dbi:ODBC:Driver={SQL Server};Server=192.168.3.137;UID=$user;PWD=$password")
You can find some more alternatives on connectionstrings.com
I am on Ubuntu (20.04) and followed the instructions to install SLQ Server in a docker container:
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-bash
I created the TestDB and the table Inventory as per the tutorial.
I installed the ODBC driver following
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
The driver library file was /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1
With that, the following script successfully connects to the DB and returns the content of the table:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $user = 'SA';
my $password = '<YourNewStrong#Passw0rd>';
my $dbh = DBI->connect("dbi:ODBC:Driver={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1};Server=localhost;Database=TestDB;UID=$user;PWD=$password");
my $sth = $dbh->prepare("SELECT * FROM Inventory");
$sth->execute();
while ( my #row = $sth->fetchrow_array ) {
print "#row\n";
}

Perl script works OK in command prompt, but fails on ODBC as a CGI script

Issue: Perl script works fine in command prompt, but fails on ODBC as a CGI script
Settings:
Win XP Pro 64 bit version 2003 SP2
Apache 2.2.17 Win32
Active Perl 5.12.2 (not Perl64)
an ODBC DSN is created for SQL SERVER
Details: from command prompt, Phone.pl runs OK - it gets query results as expected - which means DBI works fine
as a CGI script, Phone.pl prints HTML as expected before ODBC init code. (Apache settings work OK for non-ODBC script.)
here's the msg in error.log of Apache
[Tue Nov 23 13:27:57 2010] [error] [client 127.0.0.1] DBI connect('SQLSVR206','administrator',...) failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at C:/CGI/Phone.pl line 32
Any idea why it behaves differently?
#!c:/perl/bin/perl.exe
use CGI qw(:standard);
use strict;
use warnings;
use DBI;
my ($sqlstr, $sql_UNITED, $dbh206);
# 206
# select * from getProvFINON('270762789')#TIN
my #colUNITED = qw/ADR_LN_1_TXT PROV_SYS_ID ZIP_CD FULL_NM SOURCE PROV_TIN BILL_TEL/;
sub Init(){
$dbh206 = DBI->connect ("dbi:ODBC:SQLSVR206",'USER','PASSWORD',{RaiseError=>1})
or die $DBI::errstr;
$sqlstr = qq/ select * from getProvFINON(?) /; #parameters TIN
$sql_UNITED = $dbh206->prepare($sqlstr)
or die "prepare failed: " . $dbh206->errstr();
}
sub test1{
my $tin = shift();
$sql_UNITED->execute($tin) or die $sql_UNITED->errstr;
my $row = $sql_UNITED->fetchrow_hashref;
while(defined ($row)){
foreach(#colUNITED){
print $row->{$_}."~~~";
}
print "<p>";
$row = $sql_UNITED->fetchrow_hashref;
}
}
print header;
print "test text<p>";
Init();
test1('270762789');
print "999999999999<p>";
==========================================================
UPDATE: part of the problem is the 32-bit VS 64-bit ODBC issue
see http://support.microsoft.com/kb/942976
HOWEVER, after I set up ODBC, I still got following error in Apache log
[Wed Nov 24 01:38:48 2010] [error] [client 127.0.0.1] DBI connect('SQLSVR206-32','',...) failed: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'MYDOMAIN\GARY$'. (SQL-28000) at C:/CGI/test.pl line 15
I logged in as MYDOMAIN\administrator and started Apache.
ODBC was set up to use "With Windows NT authentication using the network login ID" Where does this "MYDOMAIN\GARY$" come from? Thanks!
==========================================================
FINAL UPDATE: Apache services runs using "Local System account", and that's where the "MYDOMAIN\GARY$" comes from. The thing is GARY is not with the company and his ID has been dropped from SQL Server, but ODBC code in CGI inherited GARY from Apache, resulting in error 28000.
So the problem was a convolution of
1) 32/64 bit ODBC DSN and
2) default account used by Apache
Maybe the ODBC datasource is listed under 'User DSN' and not 'System DSN'. I think that the identity that your web server runs as only has access to System DSN.
In case my post doesn't make sense, I'm editing it in order to add a link to information about the difference between User vs. System DSN's: http://www.truthsolutions.com/sql/odbc/creating_a_new_odbc_dsn.htm
Something is up with Windows 7 ODBC connections that I can't figure out either. I'd set up the ODBC and it would test fine but get a similar error as you show. Even connecting MS SQL Server Mgmt Studio to some SQL servers I have to explicitly put in extra connection settings. Since I only have the issue on some SQL servers (which I don't own) I think it's a server side problem.
On PERL i finally gave up and just created the DSN file with said extra connection settings and pointed to it directly. Note how the server name is there twice.
use DBI;
use SQL::Abstract;
my $CONNECT = "FILEDSN=thefile.dsn";
my $dbh = DBI->connect("dbi:ODBC:$CONNECT", "myidsid", "passwordThatsNotUsed");
with the ODBC file looking something like:
[ODBC]
DRIVER=SQL Server Native Client 10.0
UID=myidsid
Address=servername,3180
Network=DBMSSOCN
APP=Microsoft® Windows® Operating System
Trusted_Connection=Yes
SERVER=servername
PWD=someTextInPlaceOfPasswordAsItsNotUsed