Update row with update method

Use the update method to update the rows in the table.

$dbi->update(
  {title =>'Perl', author =>'Ken'},
  table =>'book',
  where => {id => 1},
);

In the first argument, specify the data for update as a hash reference. Specify the target table with table . Specify the condition of the row to be updated with where . Where can be a hash reference or a DBIx::Custom::Where object. The following SQL is issued:

update book set title =?, author =? where id =?;

The corresponding value is embedded in the placeholder.

If you pass a scalar reference as a value, you can embed it in SQL as it is.

{date => \ "NOW ()"}

update method options

The update method can use all the options of the "execute" method, and the following new options are available. For the execute option, refer to the option item of " execute method".

Condition specification by ID id

Use the id option to specify conditions by ID. The primary_key option must be specified.

id => 4
id => [4, 5]

For example, suppose you execute the following update statement.

$dbi->update(
  {title =>'Perl', author =>'Ken'}
  primary_key => ['id1','id2'],
  id => [4, 5],
  table =>'book'
);

This has the same meaning as the update statement below.

$dbi->update(
  {title =>'Perl', author =>'Ken'}
  where => {id1 => 4, id2 => 5},
  table =>'book'
);

prefix prefix

You can add a string between update and the table name.

prefix =>'or replace'

For example, you can execute the following SQL statement.

update or replace book

table name table

Specify the table name with the table option.

table =>'book'

Set the current time when updating mtime

To update the update time at update time, specify the column name to store the update time with the mtime option.

mtime =>'modified_time'

The default format is "YYYY-mm-dd HH: MM: SS". This can be changed with the now attribute.

Where clause where

Use the where option to write a Where clause.

where => {author =>'Ken','title' =>'Perl'}
where => [
  ['and',': author {=}',': title {like}'],
  {author =>'Ken', title =>'%Perl%'}
]

See the where option in " select method" for how to specify the where option. See Dynamic Where Clause Generation for more information on Where clause generation.

Placeholder wrapping wrap

If you want to edit the placeholder part of the set part of the update statement, use the wrap option.

wrap => {price => sub {"max ($_ [0])"}}

For example, suppose you execute the following update.

$dbi->update(
  {price => 100},
  table =>'book',
  where => {id => 1}
  wrap => {price => sub {"$_ [0] + 5"}}
);

The following SQL is executed.

update book set price =? + 5 where id = 1

Update all rows update_all

Use the update_all method to update all the rows in the table. For your safety, the update method doesn't allow all updates to the row, so use the update_all method instead.

$dbi->update_all({title =>'Perl', author =>'Ken'}, table =>'book');

Specify the data for update in the first argument as a hash reference. Specify the target table with table . The following SQL is issued:

update book set title =?, author =?;

The corresponding value is embedded in the placeholder.

Associated Information