Solved

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

Posted on 2008-11-02
14
700 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: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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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
 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

733 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