esolm has asked for the wisdom of the Perl Monks concerning the following question:
This works great until $sessionid = 10, then $sessionid++; does not increment to 11. Later in the code I insert $sessionid into a database and after 10 I get a PK violation. How do I make $sessionid increment larger than 10.$sessionid = $logquery->fetchrow_array; $sessionid = '0' if not defined $sessionid; $sessionid++;
Copy Of The Screen As The Program Runs#!/usr/bin/perl # timeline.pl use warnings; use strict; use DBI; # ####################### # STEP 1 SET THE SESSION # ####################### # here we are going to get the max session_id # from [logs].sessions and then add 1 # this will be the session of the entire process # connect to the database my ($logs, $sessionid, $timeid); $logs=DBI->connect('DBI:ODBC:claudonntLOG', 'xxxxx') || die "error opening database: $DBI::errstr\n"; # prepare the query my ($logquery); $logquery=$logs->prepare(" SELECT max (session_id) FROM sessions;") || die "Prepare failed: $DBI::errstr\n"; # prepare the timequery my ($timequery); $timequery=$logs->prepare(" SELECT GETDATE();") || die "Prepare failed: $DBI::errstr\n"; # execute the query $logquery->execute() || die "couldn't execute query: $DBI::errstr\n"; # assign sessionid $sessionid = $logquery->fetchrow_array; $sessionid = '0' if not defined $sessionid; $sessionid++; # assign timeid $timequery->execute() || die "couldn't execute query: $DBI::errstr\n"; $timeid = $timequery->fetchrow_array; # prepare the session insert query print "$sessionid $timeid\n"; my ($insert); $insert=$logs->prepare(" INSERT into sessions (session_id, session_date) VALUES ('$sessionid', '$timeid');") || die "Prepare failed: $DBI::errstr\n"; $insert->execute() || die "couldn't execute query: $DBI::errstr\n"; # clean up all the varaibles $logquery ->finish(); $timequery->finish(); $insert->finish; undef $logquery; undef $timequery; undef $insert; undef $sessionid; undef $timeid; undef $logquery;
Note that the sessionid went up each time the program ran and then hung on 10. Now i'll show you the error as the insert fails b/c of a PK violationD:\timeline>timeline.pl 8 2001-05-10 10:32:55.280 D:\timeline>timeline.pl 9 2001-05-10 10:32:56.970 D:\timeline>timeline.pl 10 2001-05-10 10:32:58.843 D:\timeline>timeline.pl 10 2001-05-10 10:33:00.867
The TableDBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server Driver][SQL +Server]Vio lation of PRIMARY KEY constraint 'pk_session'. Cannot insert duplicate + key in ob ject 'sessions'. (SQL-23000) [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been +terminated . (SQL-01000)(DBD: st_execute/SQLExecute err=-1) at D:\timeline\timeli +ne.pl line 57. couldn't execute query: [Microsoft][ODBC SQL Server Driver][SQL Server +]Violation of PRIMARY KEY constraint 'pk_session'. Cannot insert duplicate key i +n object ' sessions'. (SQL-23000) [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been +terminated . (SQL-01000)(DBD: st_execute/SQLExecute err=-1)
As always the help of the monks is apreciated.create table sessions ( session_id varchar (10), session_date datetime, file_archive varchar (25), constraint pk_session primary key (session_id))
2001-05-14 Edit by Corion : Changed title to be more descriptive
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: yesterday I posted this and you all aksed for more info so ........
by no_slogan (Deacon) on May 10, 2001 at 21:54 UTC | |
by crazyinsomniac (Prior) on May 11, 2001 at 13:26 UTC | |
by esolm (Acolyte) on May 10, 2001 at 22:47 UTC | |
|
Re: yesterday I posted this and you all aksed for more info so ........
by frankus (Priest) on May 11, 2001 at 14:38 UTC | |
|
Re: yesterday I posted this and you all aksed for more info so ........
by princepawn (Parson) on May 10, 2001 at 22:30 UTC | |
|
Re: yesterday I posted this and you all aksed for more info so ........
by SilverB1rd (Scribe) on May 11, 2001 at 00:00 UTC |