Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ 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 page


Posted by Kahenraz   (75 posts)  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?
Top

Posted by Fiendish   USA  (2,534 posts)  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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  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
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.


14,643 views.

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

Go to topic:           Search the forum


[Go to top] top

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