From: "David Armstrong" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Subject: request help internet tcp to DBI-mysql
Date sent: Thu, 8 Mar 2001 16:41:11 -0000
Hi Dave,
I am only a hobbyist, but see below...
> Could i please ask for some guidance , as i am new to perl .
Obviously.
> I wish to take a tcp connection and parse the records into fields and place
> into a mysql database
> the records are real time, comma deliminated fields, one record per line
>
> i have enclosed the program as i have it , the area i have of difficulty is
> taking the tcp feed $s into probably a buffer then i can parse the fields of
> the record into the database.
>
> any example code or help appriciated, running under activestate perl 5.6
> build 623
>
This is the code which should work in theory:
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use DBI;
my @data;
my $source_host = "linus.chemeketa.edu";
my $source_port = "10152";
### As for the database connection I cannot help with code, as the
### $dsn in your code was the table name, not the connect string
### See the DBD-Mysql docs for more information
...
my $dsn = "DBI:Mysql ??????";
my $user = "network";
my $password = "me";
...
my $dbh = DBI->connect($dsn, $user, $password);
my $sth = $dbh->prepare(q{INSERT INTO track (callsign,id,lat,lon,msg) VALUES
(?,?,?,?,?)}) or die $dbh->errstr;
my $s = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "$source_host",
PeerPort => "$source_port",
)
or die "cannot connect to $source_port at $source_host";
$s->autoflush(1);
while ( <$s> ) {
# $_ contains your data
chop;
@data = split /,/;
$sth->execute(@data) or die $sth->errstr();
}
In fact, with this code, someone sitting behind port 10152 at
linus.chemeketa.edu spits out very cryptic lines containing commas,
which indeed resemble records. I managed to split and print them to a
flat file, however, I don't believe that this is sufficient for your
needs, because there are lines with a greeting after connecting and
not all records seemed to contain the same number of commas. Thus,
there will be some more work necessary to get all the information
formatted correctly for your INSERT statement. In addition, someone
with some more experience in socket programming (which is off topic
for this list) should give you some more advice in case you need real
commmunication between your program and the service you are
connecting to.
HTH
Bodo
[EMAIL PROTECTED]