简体   繁体   English

如何在Perl中为从MySQL提取的每个记录创建唯一值?

[英]How can I create a unique value in Perl for each record I fetch from MySQL?

How can I produce a unique value key to be paired with my _rec_key_ field name? 如何产生与我的_rec_key_字段名称配对的唯一值键? I am producing a datafile. 我正在生成一个数据文件。 I was looking at using an MD5 value as my key or any other suggestions you have to making sure this is unique. 我一直在考虑使用MD5值作为密钥或您需要确保其唯一性的任何其他建议。 I'm not familiar with how to extract this value. 我不熟悉如何提取此值。

The format of the file should look something like this: 文件格式应如下所示:

__rec_key__^amd5val^ex_id^a1^einum^a2609^euser^aJoe^e^d
__rec_key__^amd5val^ex_id^a2^einum^a2609^euser^aBob^e^d

Basically, enclosing the value with ^a and ^e and ending the rec with ^d 基本上,用^a^e括起来的值,并用^d结束rec

My sample data table: 我的样本数据表:

+------+------+------+
| x_id | inum | user |
+------+------+------+
|    1 | 2608 | Joe  |
|    2 | 2609 | Bob  |
+------+------+------+

My code thus far is this...which just produces my output, without the md5 unique value. 到目前为止,我的代码是……...仅产生我的输出,而没有md5唯一值。 I would need the value to be paired with _rec_key_ 我需要将值与_rec_key_配对

my $data = '';
my $dbh = DBI->connect("DBI:mysql:test:localhost:3306");
my $sth = $dbh->prepare("select x_id, inum, user from mytest");
$sth->execute();
while (my($x_id, $inum, $user) = $sth->fetchrow_array() ) {
$data = $data. "__record_key__^a$x_id^e^a$inum^e^a$user^e^d";
}
$sth->finish;
$dbh->disconnect;
print $data;

It's difficult to understand what you're trying to do, so I can't say if using an md5 here is appropriate, but you can create them with the Digest::MD5 distribution from CPAN: 很难理解您要做什么,所以我不能说这里是否适合使用md5,但是您可以使用CPAN的Digest :: MD5发行版来创建它们:

use Digest::MD5 qw(md5_hex);

my $data = 'some arbitrary data';
my $digest = md5_hex($data);
print $digest, "\n";

prints: 印刷品:

a9959dc27c0bac23be48686ceaa1683c a9959dc27c0bac23be48686ceaa1683c

Why not UUID() in MySQL ? 为什么在MySQL中不使用UUID() Also replication safe. 复制也是安全的。

I agree with Ether. 我同意以太。 You could also use MD5() function in the database. 您还可以在数据库中使用MD5()函数。 However if your x_id isn't unique then you have to use some other value to make the MD5 with. 但是,如果您的x_id不是唯一的,则必须使用其他一些值来制作MD5。 An MD5 needs to start with unique data. MD5需要从唯一数据开始。

And if your x_id is unique, then why do you need an MD5 at all? 而且,如果您的x_id是唯一的,那么为什么根本需要MD5?

看一下Data :: UUID perl模块吗?

If you're looking to stick with your scheme, your while loop is all wrong: 如果您希望坚持自己的方案,那么while循环是完全错误的:

while (my($x_id, $inum, $user) = $sth->fetchrow_array() ) {
  $data = $data. "__record_key__^a$x_id^e^a$inum^e^a$user^e^d";
}

This will build up a very long string. 这将建立一个很长的字符串。 You actually want one string per user: 您实际上希望每个用户一个字符串:

while (my($x_id, $inum, $user) = $sth->fetchrow_array() ) {
  my $data = "__record_key__^a${x_id}^e^a${inum}^e^a${user}^e^d";
  ...
}

I've also put brackets around your variable names for clarity (ie ${user}^e^d instead of $user^e^d ). 为了清楚起见,我还在变量名前后加上了方括号(即${user}^e^d而不是$user^e^d )。

What is this record number? 这个记录号是多少? Why does it need to be unique? 为什么需要唯一? Is x_id unique? x_id是唯一的吗? Is there any format, range, or other requirements on the record number value? 记录号值是否有格式,范围或其他要求? Does the value mean anything or is it just a unique value? 该值是什么意思,还是唯一的值?

In cases like these, I like to get rid of special cases. 在这种情况下,我喜欢摆脱特殊情况。 If I need a unique string associated with each record, I let the database give each record a unique string. 如果我需要与每个记录关联的唯一字符串,则让数据库为每个记录赋予一个唯一字符串。 In database land, each record should have a primary or unique key : 在数据库域中,每个记录应具有一个主键或唯一键

 CREATE TABLE (
       pk INT AUTO_INCREMENT PRIMARY KEY,
       ...
       );

When you insert data into that table, MySQL can choose the value for pk for you. 当您将数据插入该表时,MySQL可以为您选择pk的值。

Once the value you need is stored with the other values, the meat of your program is then: 一旦将所需的值与其他值一起存储,程序的内容便变为:

my $sth = $dbh->prepare("select pk, x_id, inum, user from mytest");
$sth->execute();

while (my($pk, $x_id, $inum, $user) = $sth->fetchrow_array() ) {
    $data = $data. "$pk^a$x_id^e^a$inum^e^a$user^e^d";
    }

You haven't said much about the constraints of your problem though. 不过,您对问题的约束还没有说太多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在mysql中使用JOIN Query提取不同的值以及最后一条记录? - How can I fetch Distinct Value as well as last record using JOIN Query in mysql? 如何将唯一的记录号添加到mysql表 - How can I add a unique record number to a mysql table 如何从 Perl 连接到 MySQL? - How can I connect to MySQL from Perl? 我如何创建一个接受每个唯一记录的最后插入记录的sql? - how do i create a sql which takes the last inserted record of each unique record? 我如何才能获取过去7天中只有记录的那几天的MySQL记录,并每天返回记录呢? - How can I get MySQL records from past 7 days, but only days that have a record, and return each day as a record? 如何在 mysql 中创建过滤的唯一索引? 这是可能的? - How can I create a filtered unique index in mysql? It's possible? 我如何在mysql中使用JOIN获取不同的值? - How can I fetch Distinct Value using JOIN in mysql? 如何在MYSQL中使用COUNT和DISTINCT为每个不同的值创建单独的计数? - How can I use COUNT and DISTINCT in MYSQL to create a separate count for each distinct value? 如何检查jFrame的MySQL表中是否已经存在唯一值? - How can I check if a unique value is already in a MySQL table from jFrame? 我可以为 MySQL 中的每条记录获取唯一的 TIMESTAMP - Can I get a unique TIMESTAMP for every record in MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM