I wondering what a thread-safe way to do last_insert_row_id
would be. Currently my code looks like:
require 'sinatra'
require 'sqlite3'
db = SQLite3::Database.connect("dbname.db")
get '/' do
db.execute("INSERT INTO logs (ip,time) VALUES (?,now())",[request.ip])
return "Your row id is #{db.last_insert_row_id}"
end
It's a little silly of an example, but my point is that if two people visit at the same time, it could get into a race condition such that two people are given the same row id. How do I avoid this?
Baisicly what I'm looking for is a ruby-equivelent answer to this question: How to retrieve inserted id after inserting row in SQLite using Python?
I looked and couldn't find anything in the sqlite gem docs about a cursor, and googling brought me to the ResultSet
class which does not have a method of retrieving the last insert ID.