Solved

perl insert to database

Posted on 2008-10-07
3
552 Views
Last Modified: 2012-05-05
I have two set of values
 ABCD|CDE|EDFF

1234|321|234|

I want to insert these values using Perl script to a table in Sybase something like this:

Name      Value
ABCD      1234
CDE      1234
EDFF      1234
CDE      321
CDE      321
CDE      321
EDFF      234
EDFF      234
EDFF      234

Please advise
0
Comment
Question by:saibsk
  • 2
3 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22663572

#if $dbh is your database handle:
my $sth = $dbh->prepare('INSERT INTO table (Name, Value) VALUES (?, ?)')
  or die "Could not prepare: $DBI::errstr\n";
my $value1='ABCD|CDE|EDFF';
my $value2='1234|321|234';
 
for my $v2 (split/\|/, $value2) {
	for my $v1 (split/\|/, $value1) {
		$sth->execute($v1, $v2) or die "Could not execute: $DBI::errstr\n";
	}
}

Open in new window

0
 

Author Comment

by:saibsk
ID: 22663797
But I want o have a check on the value2 such that there whenever it is empyt i need to insert Null.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 22666156
$sth->execute($v1, $v2 or undef) or die "Could not execute: $DBI::errstr\n";
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question