UNIQUE约束失败(忽略)

时间:2017-03-30 21:59:32

标签: database perl sqlite

尝试编译数据库但在命令提示符下输出我得到以下结果:

DBD::SQLite::st execute failed: UNIQUE constraint failed: Probes.Source, Probes.Port

我理解为什么它会这样做是因为尝试将重复项添加到数据库中,但我想知道如何从命令行的输出中省略它。

当前输出的一部分:

DBD::SQLite::st execute failed: UNIQUE constraint failed: Probes.Source, Probes.Port at lab8.pl line 67, <Lab4File> line 2832.
DBD::SQLite::st execute failed: UNIQUE constraint failed: Probes.Source, Probes.Port at lab8.pl line 67, <Lab4File> line 2840.
DBD::SQLite::st execute failed: UNIQUE constraint failed: Probes.Source, Probes.Port at lab8.pl line 67, <Lab4File> line 2841.
DBD::SQLite::st execute failed: UNIQUE constraint failed: Probes.Source, Probes.Port at lab8.pl line 67, <Lab4File> line 2842.
DBD::SQLite::st execute failed: UNIQUE constraint failed: Probes.Source, Probes.Port at lab8.pl line 67, <Lab4File> line 2845.
DBD::SQLite::st execute failed: UNIQUE constraint failed: Probes.Source, Probes.Port at lab8.pl line 67, <Lab4File> line 2886.
The file contained 628 records

我希望收到的输出:

The file contained 628 records

参考代码:

# Modules used ------------------
use DBI;
use warnings;
use strict; 
# End of Modules used -----------

# Variables Used ----------------
my $DBPath;
my $RecCount;
my $ProbedDB;
my $Line;
my $ipAddr;
my $portNum;
my $insertPortDB;
my $StatHandle;
# End of Variables Used ---------



# Creats a path for the database
$DBPath = 'D:\Everything\Fleming College\Semester 2\PERL\Lab 8\lab8.db';

# Used to keep track of records in log file 
$RecCount = 0; 

# Creates database handle and connects database to be used by program
$ProbedDB = DBI->connect("dbi:SQLite:dbname=$DBPath","","")
        or die "Can't connect to the database: $DBI::errstr\n"; 

# Creates the table 
$ProbedDB->do("CREATE TABLE Probes (Source Char(15) NOT NULL,
        Port Char(5) NOT NULL, PRIMARY KEY (Source, Port))");

# Opens up the specified file that contains all of the logs used,
# if unable to open, notifies the user.
open Lab4File, "<sample.log"
    or die "Unable to open sample.log\n", $!;



# Scans text file 
while($Line = <Lab4File>)
{   
    if($Line =~ /INext-DROP/)  # Filters out the ports that were scanned 
    {
            if($Line =~ /SRC=([\d+\.]*).*DPT=([\d+\.]*)/) # Filters out the IP 
                                                          # address & the port number only
            {
                $ipAddr = $1;   # Stores the info following SRC= into variable
                $portNum = $2;  # Stores the info following DPT= into variable

                # This variable determines ports and values
                $insertPortDB = 'INSERT INTO Probes (Source, Port) Values(?,?)'; 

                # Create statement handle
                $StatHandle = $ProbedDB->prepare($insertPortDB);

                # Puts info into database
                $StatHandle->execute($ipAddr,$portNum);

                # Incremented to keep track 
                $RecCount++;
            };
    };
}
printf("The file contained %i records", "$RecCount");

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

将您的陈述更改为“INSERT OR IGNORE INTO ...”