require "sqlite3"
# Open a database
db = SQLite3::Database.new "test.db"
# Create a table
rows = db.execute <<-SQL
create table numbers (
name varchar(30),
val int
);
SQL
require "sqlite3"
db_params = {
db_name: "house-reps",
tables:
[
{
table_name: "house-reps-party",
table_fields: [
"region varchar(30)",
"congress int",
"year int",
"party varchar(30)",
"percent int",
"seats int"
]
}
]
}
def init_db(db_params)
tables = db_params[:tables]
db = SQLite3::Database.new "#{db_params[:db_name]}"
tables.each do |table|
db.execute("<<-SQL\n create table #{table[:table_name]} (\n #{table[:table_fields].join(",\n ")}\n );\nSQL")
end
end
init_db(db_params)
<<-SQL
create table party-total (
region varchar(30),
congress int,
year int,
party varchar(30),
percent int,
seats int
);
SQL
/usr/local/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize': near "<<": syntax error (SQLite3::SQLException)
from /usr/local/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'
from /usr/local/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'
from /usr/local/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
from database.rb:25:in `block in init_db'
from database.rb:23:in `each'
from database.rb:23:in `init_db'
from database.rb:29:in `<main>'
db.execute("<<-SQL\n create table #{table[:table_name]} (\n #{table[:table_fields].join(",\n ")}\n );\nSQL")
db.execute "<<-SQL\n create table #{table[:table_name]} (\n #{table[:table_fields].join(",\n ")}\n );\nSQL"
db.execute <<-SQL "\n create table #{table[:table_name]} (\n #{table[:table_fields].join(",\n ")}\n );\n" SQL
答案 0 :(得分:1)
只需删除
即可<<-SQL
SQL
这些不是字符串的一部分,而是多行字符串的“引号”。
这两个字符串都包含内容hello
"hello"
<<-SQL
hello
SQL