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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

insert SQL statement in perl

  • 23-04-2008 2:14pm
    #1
    Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭


    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,570 ✭✭✭daymobrew


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


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


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


Advertisement