Solved

How can I run update query from perl to update existing fields in column?

Posted on 2008-11-02
14
694 Views
Last Modified: 2012-05-05
I am facing problem while trying to execute update query from perl which also  insert new row with same data instead of updating only existing fields

my $diff=$dbh->prepare("UPDATE data SET diff=(time2 - time1) where username='testuser'");
$diff-> execute();

I want this script to update only existing data and not to insert new rows
0
Comment
Question by:saini_er
  • 4
  • 4
  • 3
  • +1
14 Comments
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 22865218
this is only update query... being executed..
If u want to stop insert query, comment out the execution of insert query by adding a hash in front of that.

eg.
#my $ins = $dbh->prepare("SOME INSERT QUERY");
#$ins->execute();
0
 
LVL 26

Expert Comment

by:ushastry
ID: 22865231
Try this..
$stmt = "UPDATE data SET diff=(time2 - time1) where username='testuser'";
 

# Prepare and execute the SQL query

$sth = $$dbh->prepare($$stmt) 

    || die "prepare: $$stmt: $DBI::errstr";

$sth->execute || die "execute: $$stmt: $DBI::errstr";

Open in new window

0
 

Author Comment

by:saini_er
ID: 22865235
I dont have any insert query in my code

it is just this query when executed in sql query browser just updates the field and incase executed from perl also insert the field
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22867452
Do you mean it inserts a row (record) or field?  If diff is NULL, and you want to keep it NULL, use this sql:
my $diff=$dbh->prepare("UPDATE data SET diff=(time2 - time1) where username='testuser' AND diff IS NOT NULL");
0
 

Author Comment

by:saini_er
ID: 22883424
Is there any query in sql which update all the columns in case  given user is already existing in database

for ex...if I already have 'testuser' in database and If I try to 'insert' same user again , it should update the existing user corresponding columns

I think it can be done using duplicate key but not sure
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 22883434
0
 
LVL 26

Expert Comment

by:ushastry
ID: 22883448
>>Is there any query in sql which update all the columns in case  given user is already existing in database

>>for ex...if I already have 'testuser' in database and If I try to 'insert' same user again , it should update the existing user corresponding columns

You can try REPLACE statement

http://dev.mysql.com/doc/refman/5.0/en/replace.html
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 26

Expert Comment

by:ushastry
ID: 22883452
Oops.. not refreshed..
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 22883472
saini_er, Your initial question is still confusing, what is getting inserted !!
0
 

Author Comment

by:saini_er
ID: 22883513
ghost_k8
Sorry for confusion.....my question is , I want to write a insert query(pls note it is little different from orignal question) which can check if the guven username already exist in database or not and incase it exists, it should just update  the corresponding field for that username for given values otherwsie should insert new row with that username and corresponding values
0
 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 250 total points
ID: 22883519
In simple ways you can go with a perl script only....

      my $chkSql = "select count(*) from table where username=<username's value>;";
      my $sth_chk=$dbh->prepare($chkSql);
      $sth_chk->execute();
      my $count = $sth_chk->fetchrow_array();
      print ">>".$count;
      if ($count == 0)
      {
            my $strInsert="INSERT QUERY GOES HERE";
            my $sth_Insert=$dbh->prepare($strInsert);
      }
       else
       {
          // <UPDATE QUERY EXECUTION PART GOES HERE>
       }
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 250 total points
ID: 22886370
In MySQL you can use REPLACE (instead of UPDATE or INSERT).  Here is some perl code to demonstrate this:
my $sth_InsertOrUpdate=$dbh->prepare('REPLACE INTO data (username, diff) VALUES (?, ?)');
 

#Set $username and $diff however you want

$username = 'testuser';

$diff = 123;
 

#This will insert of $username does not exist, or update if $username does exist

$sth_InsertOrUpdate->execute($username, $diff);

Open in new window

0
 

Author Comment

by:saini_er
ID: 22936877
I am using this query
 and somehow it is inserting new row instead of updating existing one
my $user=$dbh->prepare('REPLACE INTO data(username,password) VALUES (?, ?)');
$user -> execute($username,$pass);
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22939877
That means that $username for username and $pass for password are not a duplicate record.  Is username set as the primary key for the table?  If not, that would be the problem.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now