0

我以前使用 sqlite3 作为我的 ruby​​ 代码,它适用于以下代码

def existsCheck( db, id )
    temp = db.exec( 'SELECT 1 WHERE EXISTS(
        SELECT 1
        FROM Products
        WHERE promoID = ?
    ) ', [id] ).length > 0
end


def writeDB( db, product )
    db.exec( 'INSERT INTO Products ( promoID, name, price, shipping, condition, grade, included, not_included, image, time_added )
                        VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [product.promoID, product.name, product.price, product.shipping, product.condition, product.grade, product.included, product.notIncluded, product.image, product.time] )
end

Postgresql 不支持“?”的想法?还是我做错了什么?

4

1 回答 1

0

来自精美PG::Connection手册

- (PG::Result) exec(sql[, params, result_format ])
- (Object) exec(sql[, params, result_format ]) {|pg_result| ... }
[...]
PostgreSQL 绑定参数在 SQL 查询中表示为$1$1$2等。params 数组的第 0 个元素绑定到$1,第一个元素绑定到$2,依此nil类推NULL

所以你想在 PostgreSQL 中使用编号占位符:

def existsCheck( db, id )
    temp = db.exec( 'SELECT 1 WHERE EXISTS(
        SELECT 1
        FROM Products
        WHERE promoID = $1
    ) ', [id] ).to_a.length > 0
    # You'll also need to .to_a the result before you can
    # treat it as an array...
end
于 2013-01-21T01:52:53.970 回答