DBD::SQLite::st 执行失败:数据类型不匹配

发布于 2024-08-28 01:50:08 字数 1795 浏览 12 评论 0原文

这是 Perl 代码的片段:

sub insert_timesheet {
    my $dbh = shift;
    my $entryref = shift;
    my $insertme = join(',', @_);
    my $values_template = '?, ' x scalar(@_);

    chop $values_template;
    chop $values_template; #remove trailing comma

    my $insert = "INSERT INTO timesheet( $insertme ) VALUES ( $values_template );";
    my $sth = $dbh->prepare($insert);
      debug("$insert");
    my @values;
    foreach my $entry (@_){
        push @values, $$entryref{$entry}
    }
      debug("@values");
    my $rv = $sth->execute( @values ) or die $dbh->errstr;
      debug("sql return value: $rv");
    $dbh->disconnect;
}

我看到错误

DBD::SQLite::st execute failed: datatype mismatch at timesheet line 85.

$insert 的值:

[INSERT INTO timesheet( idx,Start_Time,End_Time,Project,Ticket_Number,Site,Duration,Notes ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? );]

这里是 @values:

[null '1270950742' '1270951642' 'asdf' 'asdf' 'adsf' 15 '']

这是“时间表”的架构

timesheet( 
    idx INTEGER PRIMARY KEY AUTOINCREMENT 
    , Start_Time VARCHAR
    , End_Time VARCHAR
    , Duration INTEGER
    , Project VARCHAR
    , Ticket_Number VARCHAR
    , Site VARCHAR
    , Notes VARCHAR
)

这是事情的排列方式:

----
Insert Statement
Schema
@values
----

idx
idx INTEGER PRIMARY KEY AUTOINCREMENT
null:  # this is not a mismatch, passing null will allow auto-increment.

Start_Time
Start_Time VARCHAR
'1270950742'

End_Time
End_Time VARCHAR
'1270951642'

Project
Project VARCHAR
'asdf'

Ticket_Number
Ticket_Number VARCHAR
'asdf'

Site
Site VARCHAR
'adsf'

Duration
Duration INTEGER
15

Notes
Notes VARCHAR
''

...我看不到数据类型错误-匹配。

Here's a snippit of perl code:

sub insert_timesheet {
    my $dbh = shift;
    my $entryref = shift;
    my $insertme = join(',', @_);
    my $values_template = '?, ' x scalar(@_);

    chop $values_template;
    chop $values_template; #remove trailing comma

    my $insert = "INSERT INTO timesheet( $insertme ) VALUES ( $values_template );";
    my $sth = $dbh->prepare($insert);
      debug("$insert");
    my @values;
    foreach my $entry (@_){
        push @values, $entryref{$entry}
    }
      debug("@values");
    my $rv = $sth->execute( @values ) or die $dbh->errstr;
      debug("sql return value: $rv");
    $dbh->disconnect;
}

I'm seeing the error

DBD::SQLite::st execute failed: datatype mismatch at timesheet line 85.

The value of $insert:

[INSERT INTO timesheet( idx,Start_Time,End_Time,Project,Ticket_Number,Site,Duration,Notes ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? );]

Here are @values:

[null '1270950742' '1270951642' 'asdf' 'asdf' 'adsf' 15 '']

Here's the schema of 'timesheet'

timesheet( 
    idx INTEGER PRIMARY KEY AUTOINCREMENT 
    , Start_Time VARCHAR
    , End_Time VARCHAR
    , Duration INTEGER
    , Project VARCHAR
    , Ticket_Number VARCHAR
    , Site VARCHAR
    , Notes VARCHAR
)

Here's how things line up:

----
Insert Statement
Schema
@values
----

idx
idx INTEGER PRIMARY KEY AUTOINCREMENT
null:  # this is not a mismatch, passing null will allow auto-increment.

Start_Time
Start_Time VARCHAR
'1270950742'

End_Time
End_Time VARCHAR
'1270951642'

Project
Project VARCHAR
'asdf'

Ticket_Number
Ticket_Number VARCHAR
'asdf'

Site
Site VARCHAR
'adsf'

Duration
Duration INTEGER
15

Notes
Notes VARCHAR
''

... I can't see the data-type mis-match.

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

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

发布评论

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

评论(2

往日情怀 2024-09-04 01:50:08

问题在于为 idx 插入“空”。不显式插入“idx”是可以的,自动增量会解决这个问题......

应该早点打破它。

The problem was with inserting a 'null' for idx. Not inserting 'idx' explicitly is OK, the auto-increment takes care of that...

Shoulda broken that down earlier.

夏尔 2024-09-04 01:50:08

尝试将代码缩减为更小且相似的块,看看您可以将代码缩小到多小,但仍然会出现错误(或者,看看问题在哪一步消失)。您只需使用裸字符串查询调用 execute() 即可删除大量代码,然后开始从插入语句中删除字段。

我对您正在打印的 @values 的内容有点怀疑 - 似乎每个值周围都有引号,甚至是整数,这肯定与整数类型不匹配。

Try reducing your code down to smaller and similar chunks, to see how small you can get it and still exhibit the error (or alternatively, see at what step the problem disappears). You could remove a lot of code simply by calling execute() with a bare string query, and then start removing fields from the insert statement.

I'm a little suspicious of the contents of @values that you're printing -- it appears that there are quotes around every value, even the integers, which will definitely not match an integer type.

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