473,513 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double entries

Hi,

I am running the latest MySql on a windows 200 machine. I also use the
control center gui to do all my work with. I have a Perl program that
parses online order data and then using Perl's DBI I write this data to the
database. Now, It all works fine except that each order and orders items
are written twice to the database. I have looked over my code but only see
one insert statement for order information and the ordered items. I guess I
need a fresh set of eyes to see where my error is.

I have included the code below with the database connection information
highlighted in red:
TIA

Trevor

#!/usr/local/bin/perl

#============
# Main script
#------------
use strict;
use MivaOrder;
use Data::Dumper;
#Use Perl's Database Interface (DBI) with the NySQL module to connect the
Maverick database
use DBI;
my %attr = (PrintError => 1, RaiseError => 1);
my $dbname='maverick';
my $dbuser='root';
my $dbpass='';
my
$dbh=DBI->connect('DBI:mysql:database=maverick;host=localho st;port=3306',"$d
buser","$dbpass", \%attr) || die "Unable to connect to database maverick on
localhost: $DBI::errstr\n";

my @new_orders;
#my $email = qw(c:\\maverick\\trevor_trial2.txt);
open(ORDER,$ARGV[0]) or die "Error opening \"$ARGV[0]\": $!\n";

my $order = undef;
while(<ORDER>) {

# Keep appending to the order string until we reach the end of the order.
unless(/^Site rating\.\.\: \"/) {
$order .= $_ and next;
}

# We are done with the order.

my $obj = MivaOrder->new($order);
#print Dumper(\$obj);
push @new_orders,$obj;
$order = undef;
}

my $sth1 =$dbh->prepare("INSERT INTO
miva_retail_orders(wholesale_company,order_number, date,credit_card_type,ship
ped,shiptype,bill_name,bill_company,bill_addr1,bil l_addr2,bill_city,bill_sta
te,bill_zip,bill_country,ship_name,ship_addr1,ship _addr2,ship_city,ship_stat
e,ship_zip,ship_country,email,bill_phone_number,bi ll_phone_number2,ship_phon
e_number,ship_phone_number2,sales_tax,shipping_amo unt,coupon_type,coupon_amo
unt,total) VALUES
(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ,?,?,?,?,?,?)");
foreach my $new_order(@new_orders) {

# Enter the database and write the data
$sth1->execute($new_order->wholesale_company(),$new_order->order_number(),$n
ew_order->date(),$new_order->credit_card_type(),$new_order->shipped(),$new_o
rder->ship_type(),$new_order->bill_name(),$new_order->bill_company_name(),$n
ew_order->bill_to_street(),$new_order->bill_to_street2(),$new_order->bill_to
_city(),$new_order->bill_to_state(),$new_order->bill_to_zip(),$new_order->bi
ll_to_country(),$new_order->ship_name(),$new_order->ship_to_street(),$new_or
der->ship_to_street2(),$new_order->ship_to_city(),$new_order->ship_to_state(
),$new_order->ship_to_zip(),$new_order->ship_to_country(),$new_order->email_
address(),$new_order->bill_phone_number(),$new_order->bill_phone_number2(),$
new_order->ship_phone_number(),$new_order->ship_phone_number2(),$new_order->
sales_tax(),$new_order->shipping_amount(),$new_order->coupon_type(),$new_ord
er->coupon_amount(),$new_order->total());

}

$sth1->finish;
$dbh->disconnect;

#==================
# Package MivaOrder
#------------------
package MivaOrder;
use strict;
use Carp;
use English;
use vars qw($AUTOLOAD);
my %fields = (
wholesale_company =>undef,
order_number =>undef,
date =>undef,

bill_name =>undef,
bill_email_address =>undef,
bill_phone_number =>undef,
bill_phone_number2 =>undef,
bill_business_name =>undef,
bill_to_street =>undef,
bill_to_city =>undef,
bill_to_state =>undef,
bill_to_zip =>undef,
bill_to_country =>undef,

ship_name =>undef,
ship_email_address =>undef,
ship_phone_number =>undef,
ship_phone_number2 =>undef,
ship_business_name =>undef,
ship_to_street =>undef,
ship_to_city =>undef,
ship_to_state =>undef,
ship_to_zip =>undef,
ship_to_country =>undef,

code =>undef,
name =>undef,
quantity =>undef,
price =>undef,

shipping_method =>undef,
shipping_amount =>undef,

sales_tax =>undef,
total =>undef,

notes =>undef,
coupon_type =>undef,
coupon_amount =>undef,

credit_card_type =>undef,
COD =>undef
);

sub AUTOLOAD {
my ($self,$value) = @_;
$AUTOLOAD =~ /.*::(\w+)/;
$self->{$1} = $value if($value);
return $self->{$1};
}
sub new {
my ($that,$order) = @_;
croak "Order file is undefined" if(!$order);
my $class = ref($that) || $that;
my $self = {};
$self->{$_} = $fields{$_} foreach(keys %fields);
bless $self, $class;
$self->init($order);
return $self;
}

sub init {
open(TREVOR, "> c:\\maverick\\HITHERE.txt") or die "Error opening
HITHERE.txt $!\n";
my ($self,$order) = @_;
my @lines = split(/\n/,$order);
my $in_address = undef;
my $in_order = undef;

foreach my $line(@lines) {

#our $n += 1 if $line =~/Order Number :/;
#print $n;
#next unless($line =~ /\w+/);
chomp ($line);

if($line=~ /(.*\@.*\.\w{3})\s+\"/i) {
$self->{wholesale_company} = $1;
#print $1 . "\n";
}

elsif($line =~ /Order\s+Number\s+:\s+(.*)$/i) {
$self->{order_number} = $1;
#print $1 . "\n";
}

elsif ($line =~
/Placed\s+:\s+([0-9]{2})\/([0-9]{2})\/([0-9]{4})\s+([0-9]{2}:[0-9]{2}:[0-9]{
2})/i) {
my $mon=$1;
my $day=$2;
my $year = $3;
my $time=$4;
my $date = $year . "-" . $mon . "-" . $day . " " . $time ;
$self->{date} = $date;
#print $date . "\n";
}

elsif (($line =~ /Ship To:/i) || defined($in_address)) {
$in_address = 1 and next if(!defined($in_address));
my $regex = qr#(.{0,41})(.{0,41})#;
if (!defined($self->{bill_name}))

($self->{bill_name},$self->{ship_name})
= ($line =~ /$regex/);
} elsif (!defined($self->{ship_email_address})) {
($self->{ship_email_address},$self->{bill_email_address})
= ($line =~ /$regex/);
#print $1 . "\n";
#print $2 . "\n";
} elsif (!defined($self->{ship_phone_number})) {
($self->{ship_phone_number},$self->{bill_phone_number})
= ($line =~ /$regex/);
#print $1 . "\n";
#print $2 . "\n";
} elsif (!defined($self->{ship_phone_number2})) {
($self->{ship_phone_number2},$self->{bill_phone_number2})= ($line =~
/$regex/);
# $self->{ship_phone_number2}= ($line =~
/^\(?[0-9]{3}\)?-?|s?[0-9]{3}-?|s?[0-9]$/);
#$self->{bill_phone_number2} = ($line =~
/\(?[0-9]{3}\)?-?|s?[0-9]{3}-?|s?[0-9]$/);
#print $1 . "\n";
#print $2 . "\n";
} elsif (!defined($self->{bill_business_name})
||!defined($self->{ship_business_name})) {
($self->{ship_business_name},$self->{bill_business_name})
= ($line =~ /$regex/);
#print $1 . "\n";
#print $2 . "\n";
} elsif (!defined($self->{bill_to_street})
||!defined($self->{ship_to_street}))

($self->{ship_to_street},$self->{bill_to_street})
= ($line =~ /$regex/);
#print $1 . "\n";
#print $2 . "\n";
} elsif (!defined($self->{ship_to_city})
||!defined($self->{bill_to_city})){
my ($ship_to,$bill_to) = ($line =~ /$regex/);
($self->{ship_to_zip},$self->{ship_to_state},$self->{ship_to_city} )
# = split(/\s+/,$ship_to);
# = ($ship_to =~ /^(.*)\s+([A-Za-z]{2})\s+([0-9]{5}(-[0-9]{4})?)/);#^(\w+\s+?\w+?\s+?\w+?\s+\w+)\s(\w+)\s(\d{5}-?(\d{4})?)/); #^(\w+\s?\w*.?\s?\w+)\s(\w+)\s(\d{5}(\W)?(\d{5})?)/);
= map { scalar reverse } split ' ', ( reverse $ship_to), 3;
print TREVOR $self->{order_number}. "\n";
print TREVOR $1 . "\
n";
print TREVOR $2 . "\n";
print TREVOR $3 . "\n";

($self->{bill_to_zip},$self->{bill_to_state},$self->{bill_to_city} )
#= split(/\s+/,$bill_to);
#= ($line =~
/\s{2,}(\w+.?\s?\w*.?\s?\w+.?)\s?(\w{2})\s?([0-9]{5}-?([0-9]{4})?)/);
#= ($bill_to =~
/\s{2,}(.*)\s+([A-Za-z]{2})\s+([0-9]{5}(-[0-9]{4})?)$/);#\s{2,}(\w+(\s+\w+)?
\s+\w+)\s+(\w{2})\s?([0-9]{5}-?([0-9]{4})?)/);
= map { scalar reverse } split ' ', ( reverse $bill_to), 3;
#print $1 . "\n";
#print $2 . "\n";
#print $3 . "\n";
} elsif (!defined($self->{ship_to_country})) {
($self->{ship_to_country},$self->{bill_to_country}) = ($line
=~ /$regex/);
#print $1 . "\n";
#print $2 . "\n";
$in_address = undef;
}
}

elsif (($line =~ /Code/) || defined($in_order)) {
$in_order = 1 and next if(!defined($in_order));
#if(!defined($self->{code}) && !defined($self->{quantity}) &&
!defined($self->{price})) {
if($line =~ /^(\w+)\s+(.*?)\s+(\d+)\s+(\$\d+\.\d+)/)

($self->{code},$self->{name},$self->{quantity},$self->{price}) =
($line =~ /^(\w+)\s+(.*?)\s+(\d+)\s+(\$\d+\.\d+)/);
#print $1 . "\n";
#print $3 . "\n";
#print $4 . "\n";

# Need to write items to table at this point in case we have multiple
items ordered.
#if (defined($self->{code})&&defined($self->{quantity})
&&defined($self->{price})) {
my $sth =$dbh->prepare("INSERT INTO
miva_retail_items_ordered(order_number,part_number ,quantity,price) VALUES
(?,?,?,?)");

$sth->execute($self->{order_number},$self->{code},$self->{quantity},$self->{
price});
# ($self->{code},$self->{quantity},$self->{price}) = undef;
#}
#print" Defined is: $self->{coupon_type} \n";
}elsif ($line =~ /Coupon:\s+(.*):\s+\(\$(\d+\.\d+)\)/){
#print "In coupon\n";
($self->{coupon_type},$self->{coupon_amount}) = ($line =~
/Coupon:\s+(.*):\s+\(\$(\d+\.\d+)\)/);
#print "Coupon is " . $1 . "\n";
#print "Coupon type is " . $2 . "\n";
}elsif ($line =~ /Shipping:(\s+.*:)?\s+\$(\d+\.\d+)/){
($self->{shipping_method},$self->{shipping_amount}) = ($line
=~ /Shipping:(\s+.*:)?\s+\$(\d+\.\d+)/);
#print "In shipping\n";
#print "Shipping Method is: " . $1 . "\n";
#print $2 . "\n";
}elsif ($line =~ /Sales Tax\:\s+\$(\d+\.\d+)/) {
( $self->{sales_tax}) = ($line =~ /Sales Tax\:\s+\$(\d+\.\d+)/);
# print "In sales tax: $self->{sales_tax}\n";
#print "Sales Tax is: " . $1 . "\n";

#}elsif (!defined($self->{COD})){
}elsif($line =~ /COD\s+Charge:\s+\$(\d+\.\d+)/){
($self->{COD}) = ($line =~ /COD\s+Charge:\s+\$(\d+\.\d+)/);
#print "DOC is: ". $1 . "\n";
#$in_order = undef;
}elsif ($line =~ /\s+Total:\s+\$(\d+\.\d+)/) {
$self->{total}= $1;
print PARSED "Shippint: $self->{total}\n";
} # endif

}
}

# Trim all the spaces before and after the field.
$self->{$_} =~ s/^\s+|\s+$// foreach (keys %$self);

}
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/my***********...ie.nctu.edu.tw

Jul 19 '05 #1
0 1141

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2640
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
7
2721
by: Iain King | last post by:
I have some code that converts html into xhtml. For example, convert all <i> tags into <em>. Right now I need to do to string.replace calls for every tag: html = html.replace('<i>','<em>')...
32
2124
by: Carson | last post by:
Hi , Is there a very efficient way to set a double array to 0 ? (I have tried memset, but the result doesn't look correct.) Carson
6
2950
by: ebc | last post by:
Hi, I have written a function that removes double entries from a sorted array. See the structures typedef struct tagRECR { char name;
4
2519
by: Jon Davis | last post by:
If two delegates are created that point to the exact same method, and an event is assigned both delegates, ... myObj.MyEvent += new EventHandler(MyHandler); myObj.MyEvent += new...
0
1957
by: Marcel Boscher | last post by:
Hello everybody, i get strange error messages when trying to call up my function with a SELECT functionname(with or without int); varying from: ERROR: function chr(double precision) does...
19
4202
by: Erich Pul | last post by:
hi! i got a structure, which should be filled with random integer values (it is in fact a generator for numbers like in a lotto), but these values must not be recurring, so no double occurrences...
2
1246
by: GeorgeNewbe | last post by:
Hi all, I have a table with two columns. One has ID's and the other e-mails. For one ID I can have many e-mails. The task is to create a table where I will have one column with unique entries of...
1
3033
by: windandwaves | last post by:
Hi Folk I have a databse with a bunch of double entries in some fields. For example: field A: "that is the way to go that is the way to go "
1
1859
imrosie
by: imrosie | last post by:
Please help with this one,,,,,I've been trying everything in my arsenal to fix this one. I'm stumped.... I"ve got a unbound combo box (customername) that has two events (on click); AfterUpdate and...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.