This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-16. For the latest, interactive version please visit https://mariadb.com/kb/.

insert data quickly

Below code is taking long as it is inserting line by line.

There is faster method as documented here on wrapping them using begin/end. https://mariadb.com/kb/en/library/how-to-quickly-insert-data-into-mariadb/

Can help on the syntax required to change for the code below? Thanks.

    # DB Handler objects
    my $dbh = $DBObj->connect_main();
    my $sth = $dbh->prepare("INSERT INTO ws_tbl ( ws, jobs, name, date, username) values (?,?,?,?,?)");
    
    open(my $data, '<', $filename) or die "Could not open '$filename' $!\n";
        
    while (my $line = <$data>) {
        chomp $line;
        
        if ($csv->parse($line)) {
            my @fields = $csv->fields();
            
            if( scalar(@fields) != 4 ) {
                print "@fields\n";
                next;
            } 
            
            $sth->bind_param(1, $fields[0]);
            $sth->bind_param(2, $fields[1]); 
            $sth->bind_param(3, $fields[2]);
            $sth->bind_param(4, $fields[3]);
            $sth->bind_param(5, $username);
                
            $sth->execute() || print "$. :  Error to insert values from csv file line - $line \n ";
            
        } else {
            warn "Line could not be parsed: $line\n";
        }
        
    }    
    
    $sth->finish();
    $dbh->disconnect();
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.