Lua test if table exists SQLite3

Posted by Blainer on Tue 09 Jun 2009 03:27 PM — 4 posts, 22,944 views.

#0
Anyone know how to use Lua to test if a table exists in a SQLite db?
Not the sql: CREATE TABLE IF NOT EXISTS

I want: if "table exists in database" then
#1
I use a function like this in a db class that I have


function Sqlitedb:checkfortable(tablename)
  if self:open() then
    for a in self.db:nrows('SELECT name FROM sqlite_master') do
      if a['name'] == tablename then
        self:close()
        return true
      end
    end
    self:close()
  end
  return false
end


Bast
Australia Forum Administrator #2
To save reading the whole table you could probably do something like:


SELECT * FROM sqlite_master WHERE name = 'whatever' AND type = 'table'


Then just see if you get a row back or not.
#3
Great thanks.