将多个命令插入准备好的语句 rails
Inserting multiple commands into prepared statement rails
我正在使用 rails 3 并且我需要在我的一个迁移中执行原始 sql 并且我需要使用准备好的语句来执行它,因为这是避免出现问题的最佳方法由于单身,声明等等。有没有一种方法可以在单个 prepare 语句中执行多个 sql 语句。我正在为我的数据库使用 PostgreSQL
这是我尝试过的代码
CONN = ActiveRecord::Base.connection.raw_connection
sql = %Q[
INSERT INTO table1
(
name,
email,
phone,
created_at,
updated_at
)
VALUES
(
,
,
,
current_timestamp,
current_timestamp
);
UPDATE table2
SET column_1 =
WHERE id = ;
UPDATE contacts SET
column_2 =
WHERE id =
]
CONN.prepare('insert_and_update', sql)
CONN.exe_prepared('insert_and_update', [
name,
email,
phone,
customer.id
])
但是我收到错误
cannot insert multiple commands into a prepared statement
把它变成一个命令:
with i as (
insert into table1 (
name,
email,
phone,
created_at,
updated_at
) values (
,
,
,
current_timestamp,
current_timestamp
), u as (
update table2
set column_1 =
where id =
)
update contacts
set column_2 =
where id =
;
我正在使用 rails 3 并且我需要在我的一个迁移中执行原始 sql 并且我需要使用准备好的语句来执行它,因为这是避免出现问题的最佳方法由于单身,声明等等。有没有一种方法可以在单个 prepare 语句中执行多个 sql 语句。我正在为我的数据库使用 PostgreSQL
这是我尝试过的代码
CONN = ActiveRecord::Base.connection.raw_connection
sql = %Q[
INSERT INTO table1
(
name,
email,
phone,
created_at,
updated_at
)
VALUES
(
,
,
,
current_timestamp,
current_timestamp
);
UPDATE table2
SET column_1 =
WHERE id = ;
UPDATE contacts SET
column_2 =
WHERE id =
]
CONN.prepare('insert_and_update', sql)
CONN.exe_prepared('insert_and_update', [
name,
email,
phone,
customer.id
])
但是我收到错误
cannot insert multiple commands into a prepared statement
把它变成一个命令:
with i as (
insert into table1 (
name,
email,
phone,
created_at,
updated_at
) values (
,
,
,
current_timestamp,
current_timestamp
), u as (
update table2
set column_1 =
where id =
)
update contacts
set column_2 =
where id =
;