tursodatabase / tursodatabase/libsql

Calling `Statement::excecute` multiple times does not change the parameter binding

Open
#2,135 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
17.2k
Forks
531
Avg merge
1h 12m
Merged PRs (30d)
1

Description

When calling Connection::prepare once and using the Statement to execute multiple times with varying parameters it causes the query to be executed with the first bound parameters.

Minimal reproduction example:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let database = libsql::Builder::new_local("example.db").build().await?;
    let conn = database.connect()?;
    conn.execute_batch("CREATE TABLE domain (fqdn TEXT NOT NULL PRIMARY KEY)").await?;
    let mut stmt = conn.prepare("INSERT INTO domain VALUES (?1)").await?;
    for domain in ["example.com", "example.org"] {
        println!("{}", domain);
        stmt.execute([domain]).await?;
    }
    Ok(())
}

Output:

example.com
example.org
Error: SqliteFailure(1555, "UNIQUE constraint failed: domain.fqdn")

I just released the first version of deadpool-libsql and ran into this issue in my first application I'm trying to build with libsql.

When moving the Connection::prepare call inside the loop it does the right thing. But that's really not how I would expect prepared statements to work.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal Rust reproduction using Connection::prepare and repeated Statement::execute calls with different parameters. Trace those prepare and execute entry points to find why the first binding is reused; done means the loop inserts both example.com and example.org without preparing a new statement each time.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sqlite
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.