I am using sqlite3 and attempting to pass bind variables into SQL statements in a command line application that I’m attempting as a replication of http://www.pangloss.com/seidel/shake_rule.html . This is the management utility for adding/removing from the database. I’m just trying to get an understanding of using SQL with Ruby and building a pen-and-paper rpg utility set for a DM whose game I’m currently playing.
db.execute ("INSERT INTO insults (position, string) VALUES (?, ?);", cmd_args[1].to_i, cmd_args[2])
db.execute("SELECT * FROM insults WHERE position = ? AND string = ?;", cmd_args[1].to_i, cmd_args[2]) do |output|
puts output
end
The first query causes a syntax error “unexpected ‘,’, expecting ‘)'” and more errors which follow, but they disappear when I comment this line out.
The latter query does not seem fundamentally different, but it causes no errors when I comment out the former. It still does not output anything when I set the cmd_args array values manually. The database has some test elements in it, i.e. “1|test” which do not output, though no error is thrown.
You can’t have a space between the method name and the opening parenthesis. Your first one:
is interpreted more like this:
but constructs like
(a, b, c)aren’t legal Ruby syntax in that context and there’s your syntax error.Your second one:
should work fine so, presumably, your WHERE clause doesn’t match anything so you don’t get any results. Examine the contents of
cmd_argsand make sure that:cmd_args[1..2]rather thancmd_args[0..1].