SQLite3 using a variable when creating a table?
I am hoping to pass a table name as a variable when I am creating a table with Ruby and in SQLite3.
Here is what I have so far:
$db.execute %q{
CREATE TABLE @tableName (
id integer primary key,
term varchar(100),
meaning varchar(100))
}
$db is defined as
$db = SQLite3::Database.new("test.db")
$db开发者_Python百科.results_as_hash = true
@tableName is user input. I can see that it doesn't like the @tableName, but I don't know how to work around that.
%q is a single quote alternative, interpolation does not work inside single quoted strings.
Also in ruby interpolation is done with #{}, like in "Welcome #{name}"
.
I would try:
$db.execute <<-SQL
CREATE TABLE #{@tableName} (
id integer primary key,
term varchar(100),
meaning varchar(100)
)
SQL
Just use normal string interpolation:
$db.execute %Q{
CREATE TABLE #{@tableName} (
id integer primary key,
term varchar(100),
meaning varchar(100))
}
Note that I switched your %q{}
quoting to %Q{}
to get the interpolation to work.
精彩评论