简体   繁体   English

Rails 中关于 Postgresql 的准备语句

[英]Prepared Statement on Postgresql in Rails

Right now I am in the middle of migrating from SQLite to Postgresql and I came across this problem.现在我正在从 SQLite 迁移到 Postgresql,我遇到了这个问题。 The following prepared statement works with SQLite:以下准备好的语句适用于 SQLite:

id = 5
st = ActiveRecord::Base.connection.raw_connection.prepare("DELETE FROM my_table WHERE id = ?")
st.execute(id)
st.close

Unfortunately it is not working with Postgresql - it throws an exception at line 2. I was looking for solutions and came across this:不幸的是,它不适用于 Postgresql - 它在第 2 行引发异常。我正在寻找解决方案并遇到了这个:

id = 5
require 'pg'
conn = PG::Connection.open(:dbname => 'my_db_development')
conn.prepare('statement1', 'DELETE FROM my_table WHERE id = $1')
conn.exec_prepared('statement1', [ id ])

This one fails at line 3. When I print the exception like this这个在第 3 行失败了。当我像这样打印异常时

rescue => ex

ex contains this ex 包含这个

{"connection":{}}

Executing the SQL in a command line works.在命令行中执行 SQL 有效。 Any idea what I am doing wrong?知道我做错了什么吗?

Thanks in advance!提前致谢!

If you want to use prepare like that then you'll need to make a couple changes:如果你想像这样使用prepare ,那么你需要做一些改变:

  1. The PostgreSQL driver wants to see numbered placeholders ( $1 , $2 , ...) not question marks and you need to give your prepared statement a name: PostgreSQL 驱动程序希望看到编号的占位符( $1$2 ,...)而不是问号,您需要为准备好的语句命名:

     ActiveRecord::Base.connection.raw_connection.prepare('some_name', "DELETE FROM my_table WHERE id = $1")
  2. The calling sequence is prepare followed by exec_prepared :调用顺序是prepare后跟exec_prepared

     connection = ActiveRecord::Base.connection.raw_connection connection.prepare('some_name', "DELETE FROM my_table WHERE id = $1") st = connection.exec_prepared('some_name', [ id ])

The above approach works for me with ActiveRecord and PostgreSQL, your PG::Connection.open version should work if you're connecting properly.上述方法适用于 ActiveRecord 和 PostgreSQL,如果您连接正确,您的PG::Connection.open版本应该可以工作。

Another way is to do the quoting yourself:另一种方法是自己引用:

conn = ActiveRecord::Base.connection
conn.execute(%Q{
    delete from my_table
    where id = #{conn.quote(id)}
})

That's the sort of thing that ActiveRecord is usually doing behind your back.这就是 ActiveRecord 通常在背后做的事情。

Directly interacting with the database tends to be a bit of a mess with Rails since the Rails people don't think you should ever do it.直接与数据库交互对于 Rails 来说往往有点混乱,因为 Rails 的人认为你不应该这样做。

If you really are just trying to delete a row without interference, you could use delete :如果你真的只是想在没有干扰的情况下删除一行,你可以使用delete

delete()删除()

[...] [...]

The row is simply removed with an SQL DELETE statement on the record's primary key, and no callbacks are executed.使用记录的主键上的 SQL DELETE语句简单地删除该行,并且不执行任何回调。

So you can just say this:所以你可以这样说:

MyTable.delete(id)

and you'll send a simple delete from my_tables where id = ... into the database.并且您delete from my_tables where id = ...发送一个简单的delete from my_tables where id = ...到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM