Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

insert SQL statement in perl

Options
  • 23-04-2008 3:14pm
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    i have a perl file which grabs data from one database and i want to insert it into another database.

    here's my script.
    #!/usr/bin/perl
    
    use Net::Telnet;
    use DBI;
    
    my $dsn="dbi:mysql:mydatabase:localhost:3306";
    my $dbuser="root";
    my $dbpass="";
    
    my $dbh = DBI->connect($dsn,$dbuser,$dbpass) or die "DBI::errstr\n";
    
    #At this part of the program i connect successfully to the database
    #that i want to copy the information from and store it in an array.
    #then i continue on with this foreach loop below.
    
    foreach my $line (@array)
    {
        #each line has 8 different bits of info i need, each delimited by a ~ (tilde).
        if($line =~ /~(.+)~(.+)~(.+)~(.+)~(.+)~(.)~(.+)~(.+)~/ )
        {
            $ID =$1;
            $project=$2;
            ....
            $submitDate=$8;
            #these values are correct just couldn't bother typing them all out but all eight are in INSERT statement.
        }
        
        $insertQuery = "INSERT INTO trs (ID,project,product,productNumber,slogan,status,origin,submitDate) VALUES (\'$ID\',\'$project\',\'$product\',\'$productNumber\',\'$slogan\',\'$status\',\'$origin\',\'$submitDate\')";
        $sth = $dbh->prepare($insertquery);
        $sth->execute();
    
    the query fails because there are over 1000 entries to do and many of the values being inserted could have single quotes in them (eg. Fred's going to the park) that need to stay in tact.

    how should i structure the insertquery to allow for these quotes to go in.


Comments

  • Registered Users Posts: 2,494 ✭✭✭kayos


    Escape the single quote with more single quotes i.e. Fred''s
    So just do replace on the single quote to make it a double single quote in your perl.


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    I think that you should use the quote() function.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    thanks worked a treat, please forward on credit card information so i can give you some money. :D


Advertisement