I am using perl 5.24. I am trying to learn Perl.
I had written a simple Perl code to connect to a DB. But gives error stating
DBI connect('database=vms','DBA',...) failed: (no error string) at simpleperl.pl line 13.
The code is
#!/usr/bin/perl
use DBI;
use DBD::SQLAnywhere;
my $driver = "SQLAnywhere";
my $database = "vms";
my $dsn = "DBI:$driver:database=$database";
my $userid = "DBA";
my $password = "admin";
my $dbh = DBI->connect($dsn, $userid, $password,{RaiseError => 1}) or die ("died connection:$DBI::errstr");
if($dbh)
{
print "Connection Established";
}
Can anyone point out what might be the problem here?
Note the following in DBD::SQLAnywhere documentation:
$dbh = DBI->connect( 'dbi:SQLAnywhere:ENG=demo', $userid, $passwd );
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $driver = "SQLAnywhere";
my $database = "vms";
my $dsn = "DBI:$driver:ENG=$database";
my $userid = "DBA";
my $password = "admin";
my $dbh = DBI->connect($dsn, $userid, $password, {RaiseError => 1});
print "Connection established\n";
$dbh->disconnect;
Note also the following:
Always use strict and warnings.
You do not need use DBD::SQLAnywhere;. DBI will pick the driver based on what you specify in the connection string.
You specified {RaiseError => 1} in your connection options. That means, there is no need for the or die. DBI will croak if connect fails.
You probably want AutoCommit => 0 to go with that RaiseError => 1.
There is no need for the if ($dbh) following the connection attempt. You won't get there unless connect succeeded.
Given that fixing the connection string did not solve the problem and I do not have an instance of a SQLAnywhere database to test things, I am going to recommend you add:
DBI->trace( 5 );
before the connect call, and update your question with the trace information. See also TRACING.
Related
I am using below sqlite connection string in perl to connect sqlite database and getting following below error
Can't set DBI::db=HASH(0x2c34194)->{PRAGMA journal_mode}: unrecognised attribute name or invalid value
my $driver = "SQLite";
my $database = "C:\\Sample\\Sample_Sqlite\\Activities.db3;PRAGMA journal_mode=WAL;";
my $dsn = "DBI:$driver:dbname=$database";
print $dsn;
my $dbh = DBI->connect(
$dsn,
{ RaiseError => 1 },
) or die $DBI::errstr;
Reading the section on PRAGMA in the DBD::SQLite documentation, it looks like you're setting the PRAGMA at the wrong time. It shouldn't be part of the connection string, but an SQL command that is run once you have connected.
my $driver = "SQLite";
my $database = "C:\\Sample\\Sample_Sqlite\\Activities.db3";
my $dsn = "DBI:$driver:dbname=$database";
print $dsn;
my $dbh = DBI->connect(
$dsn,
{ RaiseError => 1 },
) or die $DBI::errstr;
$dbh->do('PRAGMA journal_mode=WAL');
Update: It's probably also worth noting that the journal_mode setting is persistent. So you only need to set it once, and then you can drop it completely from your connection code.
I have the following code
#!/usr/bin/perl -w
use DBI;
use strict;
my $user = "test3";
my $password = "test3";
my $dsn = "dbi:mysql:hospital;localhost;3306";
my $dbh = DBI->connect($dsn, $user, $password);
I am getting the error
Can't locate object method "driver" via package "DBD::mysql" at /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level/DBI.pm line 821.
Perhaps the capitalisation of DBD 'mysql' isn't right. at /Users/hrai36/Documents/test2.pl line 9.
I am very new to perl. I installed DBD::mysql using cpan. I don't know what Im doing wrong.
I am new in perl, and I need to connect the database use DBI. My code as follows:
use LWP::Simple;
use XML::Simple qw(:strict);
use Data::Dumper;
use DBI;
use Getopt::Long;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use IO::File;
use warnings;
$dbh = DBI->connect("dbi:);
if (!$dbh) {
&logMsg(0, "$DBI::errstr");
die;
} else {&logMsg(0,"Connection to $dbName DB OK")}
I already set the values. Its kind like connection failed, but I didn't get any errors. I also check the log file, there is nothing showing. What can I do for checking the errors? Thanks for any comments and help.
I can't find anything wrong with your code, unless logMsg just doesn't work, but it's a tedious way to go about using DBI.
Rather than checking if something went wrong with DBI, it's much better to set DBI to throw an error. You can do this with RaiseError.
my $dbh = DBI->connect(
"dbi:ODBC:DSN=$dbName;Server=$dbHost",
$dbUser, $dbPassword,
{ RaiseError => 1 }
);
Now if DBI has a failure, including trying to connect, it will throw an error and stop the program. This avoids having to check for an error every time you use the database (you'll forget).
DBI;
$dbh = DBI->connect('Your_Database_Name', 'user_id','Password');
my $sth = $dbh->prepare ("select * from Table_name");
$sth->execute();
my #row_ary = $sth->hetshrow_array;
foreach $item (#row_ary)
{
print "$item\n";
}
print syntax not working after my $dbh = DBI->connect($dsn, "username", "password"); has been called. But when I put print syntax on the top of my $dbh = DBI->connect($dsn, "username", "password"); print work properly. This case happen when I run this code through browser using CGI and when I run this code in command-line both work properly.
Here are the code:
#!"C:\Strawberry\perl\bin\perl.exe"
use CGI qw(:standard);
use DBI;
use JSON;
print header("application/json");
my $dsn = "DBI:mysql:database=webservices;host=localhost;port=3306";
print "test"; #work properly
my $dbh = DBI->connect($dsn, "root", "bukanjombloboy");
print "test"; #not working
my $result = $dbh->prepare("SELECT * FROM news");
$result->execute();
my $json_text = to_json($result->fetchall_arrayref());
print $json_text;
$dbh->disconnect();
Sorry for my bad English, thanks anyway.
It could be a issue with the EOL, in the line:
my $dbh = DBI->connect ($dsn, "root", "bukanjombloboy");
Check all your EOL are equal in all lines.
I have found the solution.
The problem is APACHE webserver doesn't have permission to access the library so I have to run XAMPP on administrator mode.
I am trying to use DBIx::Class::Schema::Loader to create schema from an existing MySQL database. I used the make_schema_at method like this:
C:\xampp\perl\bin>perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:.\lib -e "make_schema_at('turboimmisoft::Schema', ['dbi::mysql::dbname=turboimmisoft', 'root', ''])"
where: turboimmisoft is the name of my database
I got the following error message:
Reference found where even-sized list expected at C:/xampp/perl/site/lib/DBIx/Class/Schema/Loader.pm line 165.
DBIx::Class::Storage::DBI::_connect(): You did not provide any connection_info at -e line 1
[download]
I am using ActivePerl 5.14.4 on Windows Vista and the path the the MySQL database is: "C:\xampp\mysql\data\". The path to perl.exe is: "C:\xampp\perl\bin\"
To connect to the MySQL database with DBI (not DBIx::Class), I use:
use DBI;
my $driver = "mysql";
my $database = "turboimmisoft";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password) #, {RaiseError => 1
+, AutoCommit => 1}
or die "Could not connect to database:$DBI::errstr";
Any help will be appreciated. I am new to DBIx::Class but I have been using DBI since 2007.
Do I have to create new folders in the DBIx::Class folder for the new schema?
10 months late I realize, stumbled across this searching for something else. Your make_schema_at call is wrong.
make_schema_at( $schema, \%loader_options, [$dsn, $user, $pass] );
make_schema_at documentation
you are passing in an ArrayRef where make_schema_at is expecting a HashRef.
Try
C:\xampp\perl\bin>perl
-MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:.\lib -e "make_schema_at('turboimmisoft::Schema', { debug => 1 },
['dbi:mysql:dbname=turboimmisoft', 'root', ''])"