Ruby DBI: How to add the execute arguments when use method "in (?)" -
here sample code using dbi:
begin dbh = dbi.connect("dbi:mysql:test:localhost", "testuser", "testpassword") sql = "select u.email, u.account_name, u.height, u.weight test_users id in (?) group u.id order u.id outfile '/tmp/test.csv' fields terminated ',' enclosed '\"' lines terminated '\n'" sth = dbh.prepare(sql) sth.execute('1,2,3') sth.finish rescue dbi::databaseerror => e puts "an error occurred" puts "error code: #{e.err}" puts "error message: #{e.errstr}" ensure # disconnect server dbh.disconnect if dbh end
but sql gives me like, value of "in" method incorrect:
select u.email, u.account_name, u.height, u.weight test_users id in ('1,2,3') group u.id order u.id outfile '/tmp/test.csv' fields terminated ',' enclosed '\"' lines terminated '\n'"
i want this(notice change result is: "id in (1,2,3)"):
select u.email, u.account_name, u.height, u.weight test_users id in (1,2,3) group u.id order u.id outfile '/tmp/test.csv' fields terminated ',' enclosed '\"'
that's not how in clause works, need have same amount of ?
number of arguments, in case, should have:
sql = "select u.email, u.account_name, u.height, u.weight test_users id in (?,?,?) group u.id order u.id outfile '/tmp/test.csv' fields terminated ',' enclosed '\"' lines terminated '\n'"
and pass each value query.
Comments
Post a Comment