DBI->Connect not working with mysql driver - perl

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.

Related

Argon2 encryption in perl

I'm making simple perl script for sign up/login with Argon2 for encryption. (The credentials are taken from HTML Forms). The creation of users works fine , username and hashed password are stored in the database. The problem comes with the extraction/authentication. I'm not sure I'm using the verification properly.
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::Argon2 qw/argon2id_pass argon2id_verify/;
use CGI::Simple;
use DBI;
sub get_data{
my ( $user) = #_;
my $statement = "SELECT USER_HASH FROM LOGIN_DATA WHERE USER_NAME = ?";
my $driver = "mysql";
my $database = "USERS";
my $dsn = "DBI:$driver:database=$database";
my $dataUsr = "user";
my $dataPass = "user123";
my $dbcon = DBI->connect($dsn,$dataUsr,$dataPass) or die $!;
my $preState = $dbcon->prepare($statement);
$preState->execute($user);
my #row ;
my $hash_pass;
while(#row=$preState->fetchrow_array()){
$hash_pass = $row[0];
}
return $hash_pass;
}
sub check_pass{
my ($user , $pass) = #_;
my $encoded = get_data($user);
return argon2id_verify($encoded , $pass);
}
my $cgi = CGI::Simple->new;
my $username = $cgi->param("username");
my $password = $cgi->param ("password");
check_pass($username , $password)
This are the erors when i try to run in in the terminal Use of uninitialized value in subroutine entry at checkUser.cgi line 30. Could not verify argon2id tag: Decoding failed at checkUser.cgi line 30.
Removing all the CGI, all the database connectivity and replacing the input with dummy values shows the same error message, so my guess is that you are not getting a result from the database:
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::Argon2 qw/argon2id_pass argon2id_verify/;
sub check_pass{
my ($user , $pass) = #_;
return argon2id_verify(undef, $pass);
}
check_pass("mytest", "some-test-password-2018")
__END__
Use of uninitialized value in subroutine entry at tmp.pl line 7.
Could not verify argon2id tag: Decoding failed at tmp.pl line 7.
So the best step would be for you to isolate the problem by verifying that you actually get a result from the database.

error in establishing DB connection in Perl script?

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.

Perl database connection

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";
}

Perl print syntax not working when run on browser CGI

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.

How to use DBIx::Class::Schema::Loader to create schema from an existing MySQL database?

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', ''])"