[英]How to synchronize my Perl script with MySQL query
I am new in coding in every sense of the word so kindly bear with me. 我在各种意义上的编码方面都是新手,请多多包涵。
I have created a Perl script that communicates with my PHP application but am stuck on the final hurdle of synchronizing them with MySQL queries. 我已经创建了一个Perl脚本,可以与我的PHP应用程序进行通信,但是却陷入了将它们与MySQL查询同步的最后障碍。 I want the Perl script to read table
Syncorderrelation
in my database, and to auto respond only once, sending a text message by posting the 'Auto Response' on my 'Outgoing Table') to any new entries that meets the following criteria: 我希望Perl脚本读取数据库中的表
Syncorderrelation
,并仅自动响应一次,通过将“自动响应”张贴在“发往表”上的文本消息发送到满足以下条件的任何新条目,以发送文本消息:
The entry to have an updatetype
of 1, which means the new entry is a new individual who has subscribed to a product. 该条目的
updatetype
为1,这意味着新条目是已订阅产品的新个人。
The Perl script to read the new entry of DEST_MSISDN
which is a field on the same Syncorderrelation
table (just a phone number) and to reply to the number with an automated response (just a welcome message). Perl脚本读取
DEST_MSISDN
的新条目,该条目是同一Syncorderrelation
表(仅一个电话号码)上的一个字段,并使用自动响应(仅一个欢迎消息)答复该号码。 This is done by only posting the 'Auto Response' on the 'Outgoing Table', where my PHP application takes over and sends the response on its own. 这是通过仅在“发送表”上发布“自动响应”来完成的,我的PHP应用程序将在该位置上接管并自行发送响应。
So my question is this: how do I go about creating a query whereby the Perl script will auto reply only to new entries in the Syncorderrelation
table from The current time onwards. 所以我的问题是:如何创建一个查询,使Perl脚本
Syncorderrelation
当前时间起自动答复Syncorderrelation
表中的新条目。 As currently, there are hundreds of other entries of which I don't want to trigger a response to. 与目前一样,我不想触发其他数百个条目。
But only send a reply to the New DEST_MSISDN
(phone number) and only reply to this phone number and only do so once, and thus not keep resending the same welcome message, to the same number again and again. 但是,仅发送对New
DEST_MSISDN
(电话号码)的答复,并且仅答复该电话号码,并且仅这样做一次,因此不能保持将相同的欢迎消息一次又一次地发送到相同的号码。
Note : The only way that the script can know it is a new entry, will be through the ID
Field. 注意 :脚本可以知道它是新条目的唯一方法是通过
ID
字段。 This is a unique field that is auto-generated every time a new entry enters into the Syncorderrelation
table (which will now have increased from its current number of '440' to '441'). 这是一个唯一字段,每当一个新条目进入
Syncorderrelation
表时,该字段就会自动生成(现在该表将从其当前的“ 440”数量增加到“ 441”)。
The script I have created posts correctly all the requisite fields from Syncoderrelation
table to Outgoing
table, but am unable to read the new Dest_Msisdn
(phone number) from Syncorderrelation
table and post the phone number to the Outgoing
table. 我创建的职位该脚本正确地从所有必要的字段
Syncoderrelation
表Outgoing
表,但我无法读取新Dest_Msisdn
从(电话号码) Syncorderrelation
表,并张贴的电话号码的Outgoing
表。 Without it the PHP application will not know where to send the welcome message to. 没有它,PHP应用程序将不知道将欢迎消息发送到哪里。
use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use XML::Simple;
use Digest::MD5 qw(md5_hex);
use Time::Piece;
use DBI;
use Time::HiRes qw(usleep nanosleep);
my $dest_msisdn = '';
my $link_id = '';
my $ID = '';
my $subscription_auto_reply = '';
my $user = '';
my $send_time = '';
my $now = '';
my $correlator = '2345';
my $sender_name = '30045';
my $source = '30045';
my $text_message = 'Dear Customer Welcome to our service';
my $ua = new LWP::UserAgent(keep_alive => 1);
#get all the configs
#get sms to be sent
#set the connection
my $dbh = DBI->connect('DBI:mysql:Database404', 'Kapo', 'XXXXXXXXX') || die "Could not connect to database: $DBI::errstr";
my $j = 0;
while ($j < 1) {
print "AutoResponse....\n";
my $sql = "SELECT * FROM syncorderrelation WHERE updatetype = '1'";
my $results = $dbh->selectall_hashref($sql, 'id');
foreach my $id (keys %$results) {
print "AutoResponder!!!.....\n";
#get all the required columns
#get all the required columns
my $textMessage = $results->{$id}->{text_message}; #$results->{$id}->{dest_msisdn};
my $correlator = $results->{$id}->{id};
my $senderName = $results->{$id}->{sender_name};
my $linkID = $results->{$id}->{link_id};
my $textID = $results->{$id}->{id};
my $client = $results->{$id}->{client};
my $productId = $results->{$id}->{product_id};
my $dest_msisdn = $results->{$id}->{dest_msisdn};
my $text_message = $results->{$id}->{text_message};
my $autoReply = $results->{$id}->{autoreply};
#####send service
my $user = 5;
my $send_time = time;
my $now = time;
#catch error
my $sql1 = "INSERT INTO outgoing_sms(dest_msisdn, text_message, sender_name, link_id, correlator, send_time, client, user_id, timestamp) VALUES ('$dest_msisdn', 'Dear Customer Welcome to our service', '$sender_name', '$link_id', '1161', '$now', '1', '$user', '$now')";
$dbh->do($sql1);
#print $sql1;
###send
print $sql1;
#remove from outgoing table
my $sql2 = "UPDATE incoming_sms SET sent_flag = 1 WHERE id='$correlator'";
$dbh->do($sql2);
print $sql2;
}
usleep(1000000);
}
I have many observations on your code 我对您的代码有很多观察
Always use warnings
as well as use strict
始终
use warnings
以及use strict
Don't declare all you variables at the top of the program. 不要在程序顶部声明所有变量。 They should be declared as close as possible to their first point of use
应该声明它们尽可能靠近其第一使用点
You have a use
for many modules that are unnecessary, and many variables that are unused or delcared twice. 您可以
use
许多不必要的模块,以及许多未使用或两次重复使用的变量。 It is only polite to tidy up your code before asking for help with it. 在寻求帮助之前整理一下代码是有礼貌的。 It is also much easier for you to work with a tidy program, but that is your own business.
使用整洁的程序也容易得多,但这是您自己的事。
On a similar note, don't write comments unless you have to implement code that is so complex that it is difficult to understand. 同样,除非您必须实现太复杂以至于难以理解的代码,否则不要写注释。 And say how some code works, not what it does: commenting
set the connection
before a call to DBI->connect
is ridiculous 并说说某些代码是如何工作的,而不是它的作用:在对
DBI->connect
的调用是荒谬的之前,注释set the connection
You should use DBI's prepare
and execute
instead of do
in most cases. 您应该使用DBI的
prepare
和execute
的,而不是do
在大多数情况下。 Parameters should be passed in using placeholders, not by interpolating them into the SQL string 参数应使用占位符传递,而不是通过将其插值到SQL字符串中来传递
It is best to ask the database what time it thinks it is, rather than using the local system time. 最好询问数据库它认为是什么时间,而不是使用本地系统时间。 That way the timestamps on different tables in the database can be compared properly
这样,可以正确比较数据库中不同表上的时间戳
You should put related changes to multiple tables inside a transaction so that, in the case of an error, one change cannot be made without the other 您应该对事务中的多个表进行相关更改,以便在发生错误的情况下,一个更改不能没有另一个更改
As far as I can tell you are looking for all the entries in syncorderrelation
that have an updatetype
of 1 and a corresponding row in incoming_sms
that has sent_flag
not set to 1. For this you need a JOIN
on the two tables. 至于我可以告诉你正在寻找所有的条目
syncorderrelation
有一个updatetype
的1和相应的行incoming_sms
有sent_flag
未设置为1。对于这一点,你需要一个JOIN
在两个表。
The code should look something like this. 该代码应如下所示。 (See - one module, far fewer variable declarations!) But clearly I have been unable to check it without access to test data.
(请参阅-一个模块,更少的变量声明!)但是很明显,如果没有访问测试数据,我无法检查它。
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('DBI:mysql:Database404', 'Kapo', 'XXXXXXXXX')
or die "Could not connect to database: $DBI::errstr";
my $get_new_ids = $dbh->prepare(<<__ENDSQL__);
SELECT syncorderrelation.* FROM syncorderrelation INNER JOIN incoming_sms
ON incoming_sms.id = syncorderrelation.id
WHERE syncorderrelation.updatetype = 1 AND incoming_sms.sent_flag != 1
__ENDSQL__
my $insert_outgoing = $dbh->prepare(<<__ENDSQL__);
INSERT INTO outgoing_sms (
dest_msisdn,
text_message,
sender_name,
link_id,
correlator,
send_time,
client,
user_id,
timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
__ENDSQL__
my $update_incoming = $dbh->prepare('UPDATE incoming_sms SET sent_flag = 1 WHERE id = ?');
while () {
while (my $data = $get_new_ids->fetchrow_hashref) {
my ($now) = $dbh->selectrow_array('SELECT CURRENT_TIMESTAMP');
$dbh->do('START TRANSACTION');
$insert_outgoing->execute(
$data->{dest_msisdn}, # dest_msisdn,
'Dear Customer Welcome to our service', # text_message,
$data->{sender_name}, # sender_name,
$data->{link_id}, # link_id,
1161, # correlator,
$now, # send_time,
1, # client,
5, # user_id,
$now, # timestamp
);
$update_incoming->execute($data->{id});
$dbh->do('COMMIT');
}
sleep 2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.