仅当发现重复或插入数据时如何更新记录?
在下面的代码中,有一个哈希值,其中包含带有 name
、pid
、type
和 time1
等字段的记录。 pid
和 name
是包含重复项的重复字段。
我重复发现更新需要修改的字段 否则插入,这里 name
和 pid
有重复项(重复字段)。
其余的都是独一无二的。另外,我在创建表序列号
时有一个独特的字段。我该怎么继续?我只在这段代码中插入了一个内容。我不知道如何使用 Perl 将检索到的记录存储到数组中。请指导我。
for my $test11 (sort keys %seen) {
my $test1 = $seen{$test11}{'name'};
my $test2 = $seen{$test11}{'pid'};
my $test3 = $seen{$test11}{'type'};
my $test4 = $seen{$test11}{'time1'};
print "$test11\t$test1$test2$test3$test4\n";
$db_handle = &getdb_handle;
$sth = $dbh->prepare("Select username,pid,sno from svn_log1");
$sth->execute() or die "SQL Error: $DBI::errstr\n";
my $ref = $sth->fetchall_arrayref();
print "hai";
print "****$ref";
$sth = $dbh->prepare("INSERT INTO svn_log1 values('$sno','$test11','$test1','$test4','$test2','$test3')");
$sth->execute() or die "SQL Error: $DBI::errstr\n";
}
In the code below there is a hash which contains records with fields like name
, pid
, type
and time1
.pid
and name
are repetitive fields which contain duplicates.
I duplicate found update the fields which need modification
else insert, here name
and pid
have duplicates (repetitive fields).
The rest are unique. Also I have a unique field while creating the table Serial no
. How should I go on? I have done only an insertion in this code. I dont know how to store the retrieved record into an array using Perl. Please guide me.
for my $test11 (sort keys %seen) {
my $test1 = $seen{$test11}{'name'};
my $test2 = $seen{$test11}{'pid'};
my $test3 = $seen{$test11}{'type'};
my $test4 = $seen{$test11}{'time1'};
print "$test11\t$test1$test2$test3$test4\n";
$db_handle = &getdb_handle;
$sth = $dbh->prepare("Select username,pid,sno from svn_log1");
$sth->execute() or die "SQL Error: $DBI::errstr\n";
my $ref = $sth->fetchall_arrayref();
print "hai";
print "****$ref";
$sth = $dbh->prepare("INSERT INTO svn_log1 values('$sno','$test11','$test1','$test4','$test2','$test3')");
$sth->execute() or die "SQL Error: $DBI::errstr\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你想说的是,如果数据库中已经有该名称/pid组合,你不想尝试插入一些数据,但我不能告诉,所以我帮不了你。
然而,这里有一些可以清理你的代码的事情。首先,选择合理的变量名称。其次,总是,总是,总是在 SQL 语句中使用占位符来保护它们:
假设您不想在存在“$name”和“$pid”的情况下向数据库中插入某些内容(并进行一些清理以避免准备相同的 SQL一遍又一遍):
这不是我写这篇文章的方式,但它相当清楚。我怀疑这并不完全是您想要的,但我希望它能让您更接近解决方案。
I think what you're trying to say is that you don't want to try to insert some data if you already have that name/pid combination in the database, but I can't tell, so I can't help you out there.
However, here are a few things which can clear up your code. First, choose sensible variable names. Second, always, always, always use placeholders in your SQL statements to protect them:
Assuming that you want to not insert something into the database if "$name" and "$pid" are there (and some cleanup to avoid preparing the same SQL over and over):
That's not quite the way I would write this, but it's fairly clear. I suspect it's not really exactly what you want, but I hope it gets you closer to a solution.
你想插入一些数据,但如果它存在,那么更新现有的行?
如何测试数据是否已存在于数据库中?您使用用户名和 pid 吗?
如果是这样,您可能希望更改数据库的结构:
ALTER TABLE svn_log1 ADD UNIQUE (username, pid);
这会在
username
和pid
上创建一个复合且唯一的索引。这意味着每个用户名/PID 组合必须是唯一的。这允许您执行以下操作:
INSERT INTO svn_log1 (username, pid, ...) VALUES (?, ?, ...) ON DUPLICATE KEY UPDATE time = NOW();
You want to insert some data, but if it exists, then update the existing row?
How do you test that the data already exists in the database? Are you using username and pid?
If so, you may like the change the structure of your database:
ALTER TABLE svn_log1 ADD UNIQUE (username, pid);
This create a composite, and unique index on
username
andpid
. This means that every username/pid combination must be unique.This allows you to do the following:
INSERT INTO svn_log1 (username, pid, ...) VALUES (?, ?, ...) ON DUPLICATE KEY UPDATE time = NOW();
这是什么数据库?
我的感觉是,您需要一个
UPDATE
或INSERT
查询,通常称为UPSERT
查询。如果这是 PostgreSQL,您可以创建一个
upsert
函数来处理您需要的内容。请参阅评论作为一个不错的例子。否则,在 Stack Overflow 中搜索“upsert”,您应该会找到您需要的内容。What database is this?
My feeling is that you want an
UPDATE
orINSERT
query, more commonly known as anUPSERT
query.If this is PostgreSQL you can create an
upsert
function to handle what you need. See the comments for a decent example. Otherwise, search Stack Overflow for "upsert" and you should find what you need.