duckdb-r icon indicating copy to clipboard operation
duckdb-r copied to clipboard

Prepared statement with semicolon separated SQL statements

Open ggrothendieck opened this issue 8 months ago • 4 comments

In the example below we have a string containing two semicolon-separated SQL statements. If the ? for the prepared statement is in the second SQL statement then it works but not if it is in the first.

library(duckdb)
con <- dbConnect(duckdb())
dbWriteTable(con, "mtcars", mtcars)

# 1. works (? is in second SQL statement)
rs <- dbSendQuery(con, "update mtcars set hp = 88; select * from mtcars limit ?", params = list(4))
dbFetch(rs)
##    mpg cyl disp hp drat    wt  qsec vs am gear carb
## 1 21.0   6  160 88 3.90 2.620 16.46  4  1    4    4
## 2 21.0   6  160 88 3.90 2.875 17.02  4  1    4    4
## 3 22.8   4  108 88 3.85 2.320 18.61  4  1    4    1
## 4 21.4   6  258 88 3.08 3.215 19.44  3  0    3    1

# 2. gives indicated error (? in first SQL statement)
rs <- dbSendQuery(con, "update mtcars set hp = ?; select * from mtcars limit 2", params = list(4))
## Error: rapi_prepare: Failed to execute statement update mtcars set hp = ?; select * from mtcars limit 2
## Error: Invalid Error: Expected 1 parameters, but none were supplied

dbDisconnect(con)

ggrothendieck avatar Jun 06 '24 01:06 ggrothendieck