Skip to content

Bind parameters given to Statement#execute persist beyond the call #751

Closed

Description

@flavorjones

Discussed in #740

Originally posted by jeremyevans September 7, 2026
I found this behavior surprising. I'm not sure whether it is bug, since there is a simple implementation reason for it, but I don't know whether the behavior is expected or desired. Here's a simple example:

db = SQLite3::Database.new ":memory:"
stmt = db.prepare "SELECT :a + :b"
stmt.execute(a: 1, b: 2).to_a # => [[3]]
stmt.execute(a: 2).to_a       # => [[4]]
stmt.execute(b: 3).to_a       # => [[5]]
stmt.execute.to_a             # => [[5]]

As you can see in the example, if you don't provide a bind parameter to execute, it uses the last bound value. I believe it works this way because execute calls bind_params if given any parameters, but it does not reset the preexisting bind parameter state after the statement executes.

You cannot reset the entire bind parameter state in execute, because this code is expected to work:

db = SQLite3::Database.new ":memory:"
stmt = db.prepare "SELECT :a + :b"
stmt.bind_params(a: 1)
stmt.execute(b: 2).to_a # => [[3]]
stmt.execute(b: 3).to_a # => [[4]]

My personal expectation would be that parameters given to execute only apply to that specific execution and not any subsequent executions. However, I realize that requires an allocation in the case where there are already bound parameters on the statement.

Note that this is only an issue when you have multiple execute calls on the statement, and a bind parameter set by one of the earlier execute calls is not set on a later one.

Activity

  1. added a commit that references this issue on Oct 24, 2026
    900972a
  2. flavorjones commented on Oct 24, 2026

    @flavorjones
    MemberAuthor

    After playing with this a bit, we could fix it, but I'm worried it will break code that relies on the current behavior. You're right that it comes from #execute calling #bind_params, and SQLite keeps bindings until they're cleared; I agree it's not at all obvious that #execute leaves parameters bound. Instead of changing it, I'll document it on #execute, #bind_params and ResultSet#reset, and point to Statement#clear_bindings! for anyone who wants a clean slate before each execution.

  3. added a commit that references this issue on Oct 24, 2026
    0122a01
  4. flavorjones commented on Oct 24, 2026

    @flavorjones
    MemberAuthor

    @jeremyevans FYI - documented this behavior in #760. Thanks for bringing it up!

  5. added a commit that references this issue on Oct 24, 2026
    e80ff06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions