Example of a prepared INSERT statement using ruby pg gem

后端 未结 1 1436
别跟我提以往
别跟我提以往 2021-02-05 17:29

Did some googling for about half a day and I can\'t find any sample of a prepared INSERT statement using the pg gem (postgresql ruby gem).

I tried this (after looking at

1条回答
  •  感情败类
    2021-02-05 18:13

    The pg gem wants you to use numbered placeholders ($1, $2, ...) rather than positional placeholders (?):

    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.' ])
    

    The fine manual has this to say:

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

    And again for 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.

    0 讨论(0)
提交回复
热议问题