473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1002

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

Similar topics

14
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.