I was previously using sqlite3 for my ruby code and it worked fine for the following code
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
Does Postgresql not support the idea of "?" or am I doing something wrong?
From the fine PG::Connection
manual:
- (PG::Result) exec(sql[, params, result_format ])
- (Object) exec(sql[, params, result_format ]) {|pg_result| ... }
[...]
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.nil
is treated asNULL
.
So you want to use numbered placeholders with 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