使用 Perl 的 DBI 更新行并将响应打印到 cmd 行的最佳方法?

发布于 2024-10-25 20:46:15 字数 1928 浏览 4 评论 0原文

有一些使用 DBI 模块的 Perl 代码 - (代码正在工作,如果需要,我可以在早上发布它) - 但主要是想了解 DBI 需要对行进行更新 - 并且返回错误,或确认更新已执行。

(下面只是一个基本示例,如果您愿意,请随意给出您自己的示例和示例 DDL...只想要一些我知道可以工作的代码。我已经通过 Perl PtkDB 调试器运行了我的代码,并且可以“看到”它生成和执行的 SQL - 甚至粘贴到 MySQL 控制台中并执行它...但它在 Perl 中什么也没做,即使 select 语句正在工作主要只是想更好地了解 DBI 如何处理 UPDATE 。 MySQL,如果 DBI 中有任何内置功能可以使调试变得更加简单,谢谢!)

因此,请提供一个完整的 Perl 脚本:

  • 设置连接(MySQL)
  • 根据 ID 第二行并获取第一行和最后一行。 name
  • 小写名称
  • UPDATE断开

示例表

<COL01>Id <COL02>FirstName <COL03>LastName
<ROW01-COL01>1 <ROW01-COL02>John <ROW01-COL03>Smith
<ROW02-COL01>2 <ROW02-COL02>Jane <ROW02-COL03>Doe

UPDATE (1): 有问题的代码如下。我唯一改变的是删除与问题和配置信息(例如数据库名称、用户、密码等)无关的代码,并使变量的值生成超级简单。该代码是由其他人在遗留代码库中创建的。

use strict;
use warnings;
use DBI;

sub dbOpen {
    my $dsn; 
    my $dbh;
    $dsn = "DBI:mysql:database=databasename;host=localhost;port=3306";
    $dbh = DBI->connect( $dsn, "root", "password" ) ||
    print STDERR "FATAL: Could not connect to database.\n$DBI::errstr\n";
    $dbh->{ AutoCommit } = 0;
    return($dbh);
} # END sub dbOpen

my $Data;
$Data = &dbOpen();

my ($sql,$rs,$sql_update_result);
my $column2,
my $column3;
my $id;
$column2 = 2,
$column3 = 3;
$id = 1;

$sql = "UPDATE table SET column1 = NULL, column2 = ".$column2.", column3 = ".$column3." WHERE id = ".$id.";";
$rs = $Data->prepare( $sql );
$rs->execute() || &die_clean("Couldn't execute\n$sql\n".$Data->errstr."\n" );
($sql_update_result) = $rs->fetchrow;

$Data->disconnect();

MySQL 的 DDL——如果需要,只需发表评论,我将发布一个。


更新(2):

最终找到了一个完整的示例,尽管它仅适用于 select 语句,甚至没有将任何 VAR 插入 SQL:http://search.cpan.org/~timb/DBI/DBI.pm#Simple_Examples

Have some Perl code which is using the DBI module - (the code is at work, I can post it in the morning if needed) - but mainly trying to get a sense of what DBI needs to do an update to a row -- and get either errors back, or confirmation that the UPDATE was executed.

(Below is just a basic example, feel free to give your own example and sample DDL if you want... just want some code that I know works. I've run my code via the Perl PtkDB debugger, and can "see" the SQL it generating and executing -- even paste in in the MySQL consol and execute it... but it's doing nothing in the Perl, even thought the select statements are working. Mainly just want a better idea of how DBI is handling UPDATE to MySQL, and if there's any built in feature in DBI that would make debugging this more simple. Thanks!)

So, please supply one full Perl script that:

  • Sets the connection (MySQL)
  • SELECT row two based on ID and get the first and last name
  • Lowercase the names
  • UPDATE the table
  • disconnect

Sample TABLE

<COL01>Id <COL02>FirstName <COL03>LastName
<ROW01-COL01>1 <ROW01-COL02>John <ROW01-COL03>Smith
<ROW02-COL01>2 <ROW02-COL02>Jane <ROW02-COL03>Doe

UPDATE (1): Code in question is below. The ONLY thing I've changed is remove code not related to the issue and the config info (eg database name, user, password, etc.) and made the value production for the variables super simple. This code was created by someone else and in a legacy code base.

use strict;
use warnings;
use DBI;

sub dbOpen {
    my $dsn; 
    my $dbh;
    $dsn = "DBI:mysql:database=databasename;host=localhost;port=3306";
    $dbh = DBI->connect( $dsn, "root", "password" ) ||
    print STDERR "FATAL: Could not connect to database.\n$DBI::errstr\n";
    $dbh->{ AutoCommit } = 0;
    return($dbh);
} # END sub dbOpen

my $Data;
$Data = &dbOpen();

my ($sql,$rs,$sql_update_result);
my $column2,
my $column3;
my $id;
$column2 = 2,
$column3 = 3;
$id = 1;

$sql = "UPDATE table SET column1 = NULL, column2 = ".$column2.", column3 = ".$column3." WHERE id = ".$id.";";
$rs = $Data->prepare( $sql );
$rs->execute() || &die_clean("Couldn't execute\n$sql\n".$Data->errstr."\n" );
($sql_update_result) = $rs->fetchrow;

$Data->disconnect();

DDL for MySQL -- if needed, just comment and I'll post one.


UPDATE (2):

Final found one complete example, though it's only for a select statement and not even inserting any VARs into the SQL: http://search.cpan.org/~timb/DBI/DBI.pm#Simple_Examples

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长不大的小祸害 2024-11-01 20:46:15

几乎从 DBI 复制并粘贴概要:

use DBI;
$dbh = DBI->connect($data_source, $username, $auth, \%attr);
$statement = "UPDATE some_table SET som_col = ? WHERE id = ?";
$rv  = $dbh->do($statement, undef, $som_val, $id); 
$DBI::err && die $DBI::errstr;
$rc  = $dbh->disconnect;

Almost copy and paste from DBI Synopsis:

use DBI;
$dbh = DBI->connect($data_source, $username, $auth, \%attr);
$statement = "UPDATE some_table SET som_col = ? WHERE id = ?";
$rv  = $dbh->do($statement, undef, $som_val, $id); 
$DBI::err && die $DBI::errstr;
$rc  = $dbh->disconnect;
昇り龍 2024-11-01 20:46:15

我更喜欢在更新或删除时使用 do ,因为这些操作不会返回任何行。
因此,为了进行一点调试,我会像这样修改您的代码:

my $sql = "UPDATE table SET column1=NULL, column2=$column2, column3=$column3 WHERE id=$id";
print STDERR "SQL: $sql\n"

my $numrows = $Data->do($sql);

if (not defined $numrows) {
   print STDERR "ERROR: $DBI::errstr";
} else {
   print STDERR "INFO: $numrows rows updated";
}

您可以从 Perl 代码中测量查询响应时间,但由于它是数据库的东西,我建议您使用任何 Mysql 专用工具(我不使用不使用 MySQL,抱歉)。

I prefer to use do when updating or deleting since these operations doesn't return any row.
So, in order to have a little debug, i would modify your code like this:

my $sql = "UPDATE table SET column1=NULL, column2=$column2, column3=$column3 WHERE id=$id";
print STDERR "SQL: $sql\n"

my $numrows = $Data->do($sql);

if (not defined $numrows) {
   print STDERR "ERROR: $DBI::errstr";
} else {
   print STDERR "INFO: $numrows rows updated";
}

You can measure query response times from within your perl code, but since it is a database thing, i recommend you using any Mysql specialized tool (i don't use MySQL, sorry).

就像说晚安 2024-11-01 20:46:15

您是否考虑过更高级别的东西 - 例如 DBIx::Class

Have you considered something a bit higher level - like DBIx::Class?

最丧也最甜 2024-11-01 20:46:15

您不需要返回值,在 Perl 中将它们小写,然后更新行。只需在一个 SQL 语句中执行此操作:

my $sql = "UPDATE table SET column2=lower(column2) WHERE id = ?";
$sth = $dbh->prepare($sql);
foreach my $id (@ids) {
    $sth->execute($id);
}

您还希望使用占位符来阻止 Bobby Tables 访问。

You don't need to return the values, lowercase them in Perl, then update the rows. Just do that in one SQL statement:

my $sql = "UPDATE table SET column2=lower(column2) WHERE id = ?";
$sth = $dbh->prepare($sql);
foreach my $id (@ids) {
    $sth->execute($id);
}

You also want to use placeholders to prevent Bobby Tables from visiting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文