Why do I get a syntax error with DBD::SQLite even though my version of SQLite supports `CREATE TABLE IF NOT EXISTS`?

时间:2016-04-04 16:57:09

标签: perl sqlite

I am using SQLite v3 to write a Perl script. The sqlite version is 3.3.6.

When I run sqlite on the command line it works. But when I do the same thing in Perl it raises this error

DBD::SQLite::db do failed: near "NOT": syntax error(1) at dbdimp.c line 268 at file line 2675.

This is what I do on the console:

$ sqlite3 test.db
SQLite version 3.3.6
sqlite> create table if not exists  DATA_STATUS_VALUE (TYPE TEXT PRIMARY KEY, Seq INTEGER);
sqlite> .tables AllJobs            LOCKSTAT_VALUE     test_run12_data
DATA_STATUS_VALUE  STATUS_VALUE       test_run12_lock

The version of SQLite I'm using supports IF NOT EXISTS, so why am I getting an error?

This is my Perl code:

#!/usr/bin/perl

use DBI;
my $driver = "SQLite";

$database = "test.db";
$dsn      = "DBI:$driver:dbname=$database";
$dbh      = DBI->connect( $dsn, undef, undef, { RaiseError => 1 } );

$dbh->do("CREATE TABLE IF NOT EXISTS DATA_STATUS_VALUE (TYPE TEXT PRIMARY KEY, Seq INTEGER);");

1 个答案:

答案 0 :(得分:7)

  

我正在使用的SQLite版本支持IF NOT EXISTS,为什么我会收到错误?

因为DBD :: SQLite没有使用您已安装的SQLite版本。 DBD :: SQLite与its own version of SQLite捆绑在一起;它将使用捆绑版本,除非您在编译时告诉它使用其他版本。

您可以通过运行:

找到DBD :: SQLite正在使用的SQLite版本
perl -MDBD::SQLite -le'print $DBD::SQLite::sqlite_version'

CREATE TABLE ... IF NOT EXISTS的支持为added to SQLite in v3.3.0。你应该升级DBD :: SQLite,因为最新版本(1.50)与SQLite 3.10.2捆绑在一起。

相关问题