[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  How to safely interact with an SQL database minimally

How to safely interact with an SQL database minimally

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Kahenraz   (75 posts)  [Biography] bio
Date Wed 06 Jun 2018 12:16 PM (UTC)
Message
I've run into issues in the past due to a failure to completely iterate over the result from a query. The Mush documentation clearly states:

Quote:
Note: You should let the iterator finish (that is, get to the end of the selected rows) otherwise the database remains locked. So, do not "break" out of the for loop.


On that note, is it safe to break early if I also call db:interrupt()?

Also, do I need to iterate with a for loop for db:rows() if I'm only expecting a single row?

Do I need to do this:

local a;

for _ in db:rows(Statement) do
   a = _
end -- for


Or can I just do this safely:

local a;

a = db:rows(Statement)


I can figure all of this out for myself but I can't tell how to check if the database is locked after setting up a piece of code.

What would be a good way to go about testing this?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Thu 14 Jun 2018 10:54 AM (UTC)
Message
Yeah, that warning is there because I ran into the problem accidentally a while back.

This old post might be helpful. Maybe, maybe not.
https://www.mushclient.com/forum/?id=13415&reply=5#reply5

Quote:
I can figure all of this out for myself but I can't tell how to check if the database is locked after setting up a piece of code.

What would be a good way to go about testing this?

As far as I know, the only way to check is to do another sqlite operation that will generate an error and then look at the error code.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Tue 19 Jun 2018 06:16 AM (UTC)
Message
Have you read this?

http://www.gammon.com.au/db

You can "prepare" an SQL statement, "step" once, and then finalize it.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Tue 19 Jun 2018 06:22 AM (UTC)
Message
Quote:

Also, do I need to iterate with a for loop for db:rows() if I'm only expecting a single row?


Personally I often do that, because the alternative is more coding. After all, the overhead is just one extra query for more data that immediately fails.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


12,784 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]