in reply to Re: update db problem
in thread update db problem

Adding commit didn't work unfortunately. I'm quite new to perl. I'm gonna look up at the DBI module! Thanks!

Replies are listed 'Best First'.
Re^3: update db problem
by kennethk (Abbot) on Jun 14, 2010 at 16:02 UTC
    This is not a Perl issue (perl is the program, Perl is the language). This is a database issue. As it stands, you are essentially using Perl to assemble commands for your database and then dispatching them using the command line. Therefore, if you want to debug statements, it makes sense to get them working on the command line first.
      If I run exactly the same syntax just from the command line, it does work, but as soon as I put it in $cmd from the script, it doesn't. Btw, I have tried to use DBI, but in order to set the connection there are quite a few steps which I cannot do due to some reasons :(
        The following is scrubbed code I use to get a handle to an Oracle database.

        #!/usr/bin/perl use strict; use warnings; use DBI; require DBD::Oracle; my $oracle = do{ my $db_name = 'dbname'; my $db_user = 'userid'; my $db_pass = 'password'; my @dbi_path = ("dbi:Oracle:$db_name",$db_user,$db_pass); DBI->connect( @dbi_path, { PrintError => 1, RaiseError => 1, AutoCommit => 0 } ) or die "Database connect to $db_name failed: $DBI::errstr"; };

        The code in the do block is executed, keeping the connection parameters localized to the block and returning the last thing evaluated (ie the handle). Note you will have to modify those connection parameters to reflect your case, in particular all references to Oracle should be changed to reflect your chosen technology. What brand of database are you attempting to connect to? What are the diagnostics that tell you 'it doesn't work'? If you plan on doing much work in Perl, figuring out how to work with DBI is definitely time worth while.

        On the question of the original approach, I suspect that you are not disconnecting from your database between update and select when you run the commands from the command line. If your database is set to automatically rollback changes (likely), this would cause issues just like yours to manifest. On the other hand, I believe MS SQL does not really support the rollback concept, which explains why changing technologies causes a change in behavior. What happens when you try the following in your original script:

        $cmd = $connString."update [dbname].[record] set record = 7;commit\nGO\nquit\n";