?
Solved

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

Posted on 2008-11-02
14
Medium Priority
?
703 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
14 Comments
 
LVL 21

Expert Comment

by:K V
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:Umesh
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:K V
ID: 22883434
0
 
LVL 26

Expert Comment

by:Umesh
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
 
LVL 26

Expert Comment

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

Expert Comment

by:K V
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:
K V earned 1000 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 1000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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