英文:
Mysql2 Ruby: No implicit conversion Array/Hash - INSERT statement
问题
我正在尝试学习mysql2 Ruby gem的用法,我遇到了这个问题:
无法将数组隐式转换为哈希 (TypeError)
在执行INSERT查询时:
client.query("CREATE TABLE IF NOT EXISTS test(Name TEXT, Value INT)")
client.query("INSERT INTO test VALUES (?, ?)", ["abc", 3])
我之前使用sqlite,并且使用相同的语法一直正常工作。不明白这里发生了什么...
英文:
I'm trying to learn mysql2 ruby gem usage and I have encoutered this problem:
> no implicit conversion of Array into Hash (TypeError)
When executing the INSERT query:
client.query("CREATE TABLE IF NOT EXISTS test(Name TEXT, Value INT)")
client.query("INSERT INTO test VALUES (?,?)", ["abc",3])
I'm coming from sqlite, and using the same syntax was working fine. Can't understand what's happening here...
答案1
得分: 1
你可以使用已经准备好的语句来实现这个(如在 mysql2 gem GitHub 页面上记录的)。
statement = client.prepare("INSERT INTO test (Name, Value) VALUES (?,?)")
statement.execute("abc", 3)
英文:
You can use prepared statemets for this (as documented on mysql2 gem github page)
statement = client.prepare("INSERT INTO test (Name, Value) VALUES (?,?)")
statement.execute("abc", 3)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论