Mysql2 Ruby: 无隐式转换 Array/Hash – INSERT 语句

huangapple go评论64阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年3月3日 19:48:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定