mardi 26 avril 2016

create table with foreign key constrain in SQLite using Perl

I'm trying to create a database with two tables: customer and transaction (one customer can do many transactions, one transaction can only be done by one customer). I'm using the following code in Perl:

my $dbh = DBI->connect("dbi:SQLite:dbname=$RealBin/active.db","","") or die $DBI::errstr;

my @ddl=
("CREATE TABLE IF NOT EXISTS customer (
  id INTEGER PRIMARY KEY UNIQUE,
  id_1 INTEGER,
  id_2 INTEGER,
  name TEXT
)",
"CREATE TABLE IF NOT EXISTS transaction (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  customer_id INTEGER REFERENCES customer(id),
)");

for my $sql(@ddl){
  $dbh->do($sql);
}

When I run my script, I'm getting the following error:

DBD::SQLite::db do failed: near "transaction": syntax error at
    t/oneToMany.t line 28 (#1)
DBD::SQLite::db do failed: near "transaction": syntax error at t/oneToMany.t line 28.
DBD::SQLite::db do failed: near "transaction": syntax error at
    t/oneToMany.t line 38 (#1)

The line number 28 is the following one: $dbh->do($sql);

Can you please tell me what I'm doing wrong? Thank you very much in advance...

Aucun commentaire:

Enregistrer un commentaire