According to this post the following SQL statements should give me a vector
1, 2, 2, 2, 2
in the end:
require("RMySQL")
con <- dbConnect(
dbDriver("MySQL"),
db="your_db",
user="your_user",
password="your_pw",
host="localhost"
)
> con
<MySQLConnection:(6640,122)>
> dbSendQuery(con, "DROP TABLE IF EXISTS t;")
<MySQLResult:(6640,122,0)>
> dbSendQuery(con, "CREATE TABLE t (i INT NOT NULL AUTO_INCREMENT PRIMARY KEY);")
<MySQLResult:(6640,122,1)>
> dbSendQuery(con, "INSERT INTO t VALUES(NULL);")
<MySQLResult:(6640,122,2)>
> dbGetQuery(con, "SELECT LAST_INSERT_ID() FROM t;")
LAST_INSERT_ID()
1 0
> dbSendQuery(con, "INSERT INTO t VALUES(NULL),(NULL),(NULL);")
<MySQLResult:(6640,122,3)>
> dbGetQuery(con, "SELECT LAST_INSERT_ID() FROM t;")
LAST_INSERT_ID()
1 0
2 0
3 0
4 0
Adapted the example to have it resemble real use cases a bit more than the original one:
dbSendQuery(con, "DROP TABLE IF EXISTS t;")
dbSendQuery(con, paste("CREATE TABLE t",
"(i INT NOT NULL AUTO_INCREMENT PRIMARY KEY, x INT);"))
> dbGetQuery(con, "SELECT CONNECTION_ID();")
CONNECTION_ID()
1 673490
dbSendQuery(con, "INSERT INTO t SET x=1;")
> dbGetQuery(con, "SELECT CONNECTION_ID();")
CONNECTION_ID()
1 673491
> dbGetQuery(con, "SELECT LAST_INSERT_ID();")
LAST_INSERT_ID()
1 0
> dbGetQuery(con, "SELECT CONNECTION_ID();")
CONNECTION_ID()
1 673493
> dbGetQuery(con, "SELECT LAST_INSERT_ID();")
LAST_INSERT_ID()
1 0
dbSendQuery(con, "INSERT INTO t SET x=2;")
> dbGetQuery(con, "SELECT LAST_INSERT_ID();")
LAST_INSERT_ID()
1 0
> dbGetQuery(con, "SELECT * FROM t;")
i x
1 1 1
2 2 2
Well, it doesn't, really ;-)
I've googled a bit and AFAIU, LAST_INSERT_ID()
is "connection-aware" in the sense that the same connection must be used if it is to work properly. However, I thought that by assigning the connection object to con
I was making sure that indeed the same connection is used in each of the statements above.
Well, apparently not ;-) Can anyone help me out with some explanations and/or workarounds?
Using something like select max(<ID>) from <TABLE>
isn't going to cut it, though, as I'm running multiple threads that simultaneously write to the DB, thus messing up ID retrieval if done that way.
Thanks!
RMySQL
functions don't really care about the explicit conn
argument that much but open new connections in the background every time you connect to the DB. Probably some good reasons for this, too. Yet, does anyone know how to avoid this? As suggested by Jeff
> dbGetInfo(con)
$host
[1] "localhost"
$user
[1] "your_user"
$dbname
[1] "your_db"
$conType
[1] "localhost via TCP/IP"
$serverVersion
[1] "5.5.20"
$protocolVersion
[1] 10
$threadId
[1] 673489
$rsId
$rsId[[1]]
<MySQLResult:(6640,171,3)>
> dbGetInfo(dbDriver("MySQL"))
$drvName
[1] "MySQL"
$connectionIds
$connectionIds[[1]]
<MySQLConnection:(6640,149)>
$connectionIds[[2]]
<MySQLConnection:(6640,112)>
$connectionIds[[3]]
<MySQLConnection:(6640,171)>
$fetch_default_rec
[1] 500
$managerId
<MySQLDriver:(6640)>
$length
[1] 16
$num_con
[1] 3
$counter
[1] 179
$clientVersion
[1] "5.5.20"
> dbListConnections(dbDriver("MySQL"))
[[1]]
<MySQLConnection:(6640,149)>
[[2]]
<MySQLConnection:(6640,112)>
[[3]]
<MySQLConnection:(6640,171)>
I found a working solution here. It's also mentioned in stephan mc's reply, but as the second option. The first one didn't work for me, so I figured this might be worth highlighting more.
Anyways, the trick is to run dbClearResult()
between the INSERT
and SELECT LAST_INSERT_ID()
:
> library("RMySQL")
> con <- dbConnect(MySQL())
> dbSendQuery(con, "DROP TABLE IF EXISTS t;")
> dbSendQuery(con, "CREATE TABLE t (i INT NOT NULL AUTO_INCREMENT PRIMARY KEY);")
> res <- dbSendQuery(con, "INSERT INTO t VALUES (NULL);")
# doesn't work:
> dbGetQuery(con, "SELECT LAST_INSERT_ID();")
LAST_INSERT_ID()
1 0
# works:
> dbClearResult(rs)
> dbGetQuery(con, "SELECT LAST_INSERT_ID();")
LAST_INSERT_ID()
1 1