Re: perl with mysql which takes a file as a input that contains t he info to create the tables



On Nov 16, 2007 1:52 AM, Pradeep Kumar <prady184u@xxxxxxxxx> wrote:
snip
NOW I WANTED TO KNOW THE PROCEDURE FOR TAKING THE CODE AS INPUT FROM A
TEXT FILE & CREATE THE SPECIFIED TABLE
snip

Don't yell.

Warning, this is untested code and may blow up in your face. Second
warning, this code is very inefficient due to its use of AutoCommit,
this should be set to 0 and you should be committing after every
create table and after every thousand or so inserts (I was just too
lazy to put that in). Third warning, I have not scanned the code for
stupidity (I already see at least one: I don't have to use $1, $2, I
could do the assignment in the if).

#!/usr/bin/perl

use warnings;
use strict;

my $dbh = DBI->connect(
'dbi:mysql:dbname=foo',
'',
'',
{
AutoCommit => 1,
ChopBlanks => 1,
RaiseError => 1,
PrintError => 0,
}
) or die DBI->errstr;

my @cols;
my $insert;
while (<DATA>) {
chomp;
if (/^(\w+)>(.*)/) {
my ($tabname, $tabbody) = ($1, $2);
$dbh->do("CREATE TABLE $tabname ( $tabbody )");
@cols = map { (split " ")[0] } split ",", $tabbody;
$insert = $dbh->prepare("insert into $tabname (" .
(join ", ", @cols) . ") values (" .
(join ", ", (('?') x @cols)) . ")");
next;
}
die "corrupt file" unless @cols;
$insert->execute(split ",");
}

$dbh->disconnect;

__DATA__
Details> name VARCHAR(12), id INTEGER, area VARCHAR(32)
prady,2039,india
sandy,2398,india
sam,1234,aussie
Rob,2345,Eng
extraDetails>name1 VARCHAR(12), name2 VARCHAR(12)
prady,sandy
sandy,Rob
Rob,sam
sam,prady
.



Relevant Pages

  • Re: BARRY & THE BROTHERS TO THE RESCUE SHOOTDOWN
    ... [snip portions of Kenny's posting already debunked here, ... It is an "advisory" NOT a warning to depart or not enter. ... warnings prior to the flight. ... >>> Look at the LOGS Danny they state the EXACT times. ...
    (soc.culture.cuba)
  • Re: Identify source of warning
    ... post because I wasn't saying I thought using undef there was the way ... I did that to try to trip the same warning output. ...
    (perl.beginners)
  • Re: Does anyone know what this text means? (related to C4251 warning)
    ... It isn't type derivation, but it is inheritance in the OOP sense of the ... It doesn't just "avoid the warning". ... Every single use of dllimport/dllexport on a type violates the ODR. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: 2 problems parsing output from HTML::TableExtract
    ... characters in the string that, while not a problem in browsers ... Here is the code block the warning relates to: ... foreach $ts { ...
    (comp.lang.perl.misc)
  • lxrun and RH 7.3 or something else
    ... warning: /etc/ld.so.conf created as /etc/ld.so.conf.rpmnew ... GNU gdb 5.0 ... Do I really have to have Sun Linux 5.0 in order to run lxrun? ...
    (comp.unix.solaris)