Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

insert SQL statement in perl

  • 23-04-2008 03:14PM
    #1
    Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 6,651 ✭✭✭daymobrew


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


  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


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


Advertisement