0

我想将一个表 Im 建筑物的主键设置为 3 个整数的复合键。我在这样的 Ruby 脚本中执行此操作:

  ...Ruby Script
  .............

  .............
  #Begin database operations
  begin
  db = SQLite3::Database.new ("#{filename.split('.')[0]}" + ".db")

  db.execute ("CREATE TABLE IF NOT EXISTS AIRED (programCode INTEGER , fromDate INTEGER, 
  toDate INTEGER, PRIMARY KEY(programCode,fromDate , toDate), program TEXT, channel  TEXT, 
   weekday TEXT, startTime TEXT, endTime TEXT, duration TEXT)")

  rescue SQLite3::Exception => e
  puts "Exception occured"
  puts e.message
  puts e.backtrace

  ensure
  db.close if db

  end

   .........

当我运行此脚本时,我在终端收到以下错误

      Exception occured
      near "program": syntax error
      /Users/AM/.rvm/gems/ruby-2.0.0-p195/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize'
      /Users/AM/.rvm/gems/ruby-2.0.0-p195/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `new'
     /Users/AM/.rvm/gems/ruby-2.0.0-p195/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `prepare'
     /Users/AM/.rvm/gems/ruby-2.0.0-p195/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:134:in `execute'
      csv_parser.rb:60:in `<main>'

我查看了这篇SO 帖子,了解如何制作复合键:

我究竟做错了什么?

谢谢

4

1 回答 1

1

您不能在这样的列定义结束之前放置表约束。你需要写这个

db.execute <<__SQL__
CREATE TABLE IF NOT EXISTS AIRED (
  programCode INTEGER,
  fromDate    INTEGER,
  toDate      INTEGER,
  program     TEXT,
  channel     TEXT,
  weekday     TEXT,
  startTime   TEXT,
  endTime     TEXT,
  duration    TEXT,
  PRIMARY KEY (programCode, fromDate, toDate)
)
__SQL__
于 2013-07-14T13:11:32.890 回答