Solved

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

Posted on 2008-11-02
14
696 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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