Page 1 of 1
How to use db:_begin(), db:_commit(), db:_end() ?
Posted: Thu Mar 11, 2021 3:50 pm
by visionok
I want to do a bunch of inserts into a database, if I do them one by one, it takes quite a while (seconds or more,lots of disk thrashing) for relatively few entries (eg 10's of entries). So I surrounded the insertions/updates with db:_begin(), db:_commit(), db:_end()
local dbh = db:get_database("database"), res
if map_room_count > db_room_count
then
update_db = true
display("Updating DB")
db:_begin()
local i, r
for i, r in pairs(endresult)
do
res = db:add(dbh.area_rooms, {room = r, id = i, area=area.current})
if res == nil
then
display("error adding room")
end
end
db:_commit()
db:_end()
end
but that throws up an error
attempt to call method '_begin' (a nil value)>
Am I using db:_begin(), db:_commit(), db:_end() for their intended purpose and am I using them correctly?
Re: How to use db:_begin(), db:_commit(), db:_end() ?
Posted: Sat Mar 13, 2021 8:39 am
by Vadi
Hey - check out
https://wiki.mudlet.org/w/Manual:Scripting#Transactions, you're supposed to use the actual db name in there.
I've updated the examples to be clearer
Re: How to use db:_begin(), db:_commit(), db:_end() ?
Posted: Sat Mar 13, 2021 9:06 am
by visionok
Yeah, the updates are
much quicker now, thanks.
if map_room_count > db_room_count
then
update_db = true
display("Updating DB")
dbh:_begin()
local i, r
for i, r in pairs(endresult)
do
res = db:add(dbh.area_rooms, {room = r, id = i, area=area.current})
if res == nil
then
display("error adding room")
end
end
dbh:_commit()
dbh:_end()
end
Re: How to use db:_begin(), db:_commit(), db:_end() ?
Posted: Sat Mar 13, 2021 9:56 am
by Vadi
Glad you got it working!