gpt4 book ai didi

ruby - 使用 ruby​​ pg gem 准备好的 INSERT 语句示例

转载 作者:数据小太阳 更新时间:2023-10-29 06:42:38 25 4
gpt4 key购买 nike

谷歌搜索了大约半天,我找不到任何使用 pg gem(postgresql ruby​​ gem)准备好的 INSERT 语句的示例。

我试过了(在查看了 gem 文档之后):

def test2
conn = PG.connect( dbname: 'db1' )
conn.prepare("statement1", 'INSERT INTO table1 (id, name, profile) VALUES (?, ?, ?)')
end

但是我得到以下错误:

pgtest.rb:19:in `prepare': ERROR:  syntax error at or near "," (PG::Error)
LINE 1: INSERT INTO table1 (id, name, profile) VALUES (?, ?, ?)
^
from pgtest.rb:19:in `test2'
from pgtest.rb:25:in `<main>'

最佳答案

pg gem 希望您使用编号占位符($1$2、...)而不是位置占位符():

conn = PG.connect(:dbname => 'db1')
conn.prepare('statement1', 'insert into table1 (id, name, profile) values ($1, $2, $3)')
conn.exec_prepared('statement1', [ 11, 'J.R. "Bob" Dobbs', 'Too much is always better than not enough.' ])

fine manual有这样的话:

- (PGresult) prepare(stmt_name, sql[, param_types ])
[...]
PostgreSQL bind parameters are represented as $1, $1, $2, etc., inside the SQL query.

再次为 exec_prepared :

PostgreSQL bind parameters are represented as $1, $1, $2, etc., inside the SQL query. The 0th element of the params array is bound to $1, the 1st element is bound to $2, etc.

关于ruby - 使用 ruby​​ pg gem 准备好的 INSERT 语句示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10841390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com