Ekanvitha9 has asked for the wisdom of the Perl Monks concerning the following question:

I'm passing the data into MySQL using Perl code.While passing the values dynamically, the values are not inserting in the database.Here is my DB connectivity code

#!C:\Perl\bin\perl.exe use strict; use warnings; use DBI; my $session_id = 723; print $session_id . "\n"; my $employeename='Anvi'; print("Employee name: $employeename\n"); my $dbh = DBI->connect("DBI:ODBC:DSN=databasename;uid=myusername;pwd=m +ypassword") or die $DBI::errstr; my $sth=$dbh->prepare("INSERT INTO employee_details (session_id,Name) +VALUES (?,?)"); $sth->execute; $dbh->disconnect; print "Done\n";

Replies are listed 'Best First'.
Re: Perl with Insert query
by hippo (Archbishop) on Jul 29, 2019 at 10:10 UTC

    You have not bound any arguments to your placeholders. Neither have you checked the results of any of your DBI method calls which would have alerted you to there being a problem.

    my $res = $sth->execute ($session_id, $employeename); warn "Cannot insert $session_id, $employeename into employee_details ( +res is $res): " . $dbh->errstr unless $res;
Re: Perl with Insert query
by haj (Vicar) on Jul 29, 2019 at 10:11 UTC

    You just need to pass the values to the call to execute:

    $sth->execute($session_id,$employeename)
Re: Perl with Insert query
by Tux (Canon) on Jul 29, 2019 at 10:24 UTC

    Apart from the two perfect answers you already got, I wonder why you are using DBD::ODBC instead of DBD::MariaDB or DBD::mysql. Could you give a reason, so I can learn?


    Enjoy, Have FUN! H.Merijn
Re: Perl with Insert query
by erix (Prior) on Jul 29, 2019 at 10:46 UTC

    I think you need a $dbh->commit();

    after the execute.