我想将tabe 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>'
我看了this SO帖子,看看如何制作复合键:
我做错了什么?
由于
答案 0 :(得分: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__