Theopile
Theopile

Reputation: 918

Check If Database is Open in Lua

How can I check with Lua to see if my connection to my sqlite database is still open?

Upvotes: 1

Views: 825

Answers (1)

jpjacobs
jpjacobs

Reputation: 9549

LuaSQLite and LuaSQL are the most widely spread SQLite libraries for Lua.

Using luasqlite it is:

sqlite3=require"sqlite3"
db1=sqlite3.open_memory()
db2=sqlite3.open_memory()
db2:close()
print("db1 is ".. (db1:isopen() and "is open" or "it's not open"))
print("db2 is ".. (db2:isopen() and "is open" or "it's not open"))

Using luasql with the sqlite backend:

sqlite3=require('luasql.sqlite3')
env=sqlite3.sqlite3()
con1=env:connect(':memory:')
con2=env:connect(':memory:')
con2:close()
print("con1 is ".. (tostring(con1):match'closed' and "not open" or "open"))
print("con2 is ".. (tostring(con2):match'closed' and "not open" or "open"))

Upvotes: 4

Related Questions