我希望能通過一個表名作爲變量,當我用Ruby和SQLite3創建一個表。SQLite3創建表時使用變量?
這是我到目前爲止有:
$db.execute %q{
CREATE TABLE @tableName (
id integer primary key,
term varchar(100),
meaning varchar(100))
}
$ DB定義爲
$db = SQLite3::Database.new("test.db")
$db.results_as_hash = true
@tablename是用戶輸入。我可以看到它不喜歡@tableName,但我不知道如何解決這個問題。
非常感謝!我嘗試使用#{@ tableName},但不斷收到錯誤,這使我懷疑#{@ tableName}方法並促使我發佈。感謝您提供%Q {}提示! – Naz 2011-05-10 00:19:55
@Naz:'%q {}'就像單引號,'%Q {'''就像雙引號。 – 2011-05-10 00:51:25
非常感謝這兩個評論 - 它現在肯定更有意義! – Naz 2011-05-10 02:11:21