1

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Alex Nye
  • 147
  • 8

1 Answers1

4

You can't have a space between the method name and the opening parenthesis. Your first one:

db.execute ("INSERT INTO insults (position, string) VALUES (?, ?);",    cmd_args[1].to_i, cmd_args[2])

is interpreted more like this:

x = ("INSERT ....", cms_args[1].to_i, cmd_args[2])
db.execute x

but constructs like (a, b, c) aren't legal Ruby syntax in that context and there's your syntax error.

Your second one:

db.execute("SELECT * ...", cmd_args[1].to_i, cmd_args[2]) do |output|
  puts output
end

should work fine so, presumably, your WHERE clause doesn't match anything so you don't get any results. Examine the contents of cmd_args and make sure that:

  1. You don't have any stray whitespace.
  2. Your arguments really are in cmd_args[1..2] rather than cmd_args[0..1].
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • That feeling where you're relieved and frustrated at its simplicity. Thank you for the added understanding of just why that space caused trouble. I'll have to keep looking for the second problem, but thanks. – Alex Nye Aug 27 '12 at 07:01
  • 1
    @bismuth: The space issue is related to being able to call methods without parentheses in Ruby. – mu is too short Aug 27 '12 at 07:05