saini_er
asked on
How can I run update query from perl to update existing fields in column?
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("UPDAT E 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
my $diff=$dbh->prepare("UPDAT
$diff-> execute();
I want this script to update only existing data and not to insert new rows
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";
ASKER
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
it is just this query when executed in sql query browser just updates the field and incase executed from perl also insert the field
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("UPDAT E data SET diff=(time2 - time1) where username='testuser' AND diff IS NOT NULL");
my $diff=$dbh->prepare("UPDAT
ASKER
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
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
>>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
>>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
Oops.. not refreshed..
saini_er, Your initial question is still confusing, what is getting inserted !!
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am using this query
and somehow it is inserting new row instead of updating existing one
my $user=$dbh->prepare('REPLA CE INTO data(username,password) VALUES (?, ?)');
$user -> execute($username,$pass);
and somehow it is inserting new row instead of updating existing one
my $user=$dbh->prepare('REPLA
$user -> execute($username,$pass);
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.
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();