I want to create a function to find the column names in a table. That isn’t difficult if there are at least one row in the table. But what to do when the table is empty? I could retrieve the sql query that created the table (from sqlite_master), but it seems to be a hard job to analyse a string like this:
Create table Custommers(Id integer primary key, Account integer, Company text, Region text, LastPurchase text, Amount numeric(10,2))
The number of columns can be found using the code below, but to find the names (and preferably the type) is a more complex task.
Is there an easier method to determine the table structure?
Code:function SQLdb.StructFieldCount(sDB, sTable) --[[ Function: SQLdb.StructFieldCount Purpose: Determine number of columns in the table by reading sqlite_master (can be used even when there are no records in the table) Arguments: (database) The handle to the database that you want to operate on. (string) Name of table Returns: (number) or nil (if unsuccessful) ]] nFieldCounter = nil; SQLite.Compile(sDB, "SELECT sql FROM sqlite_master WHERE type='table'"); repeat SQLite.Step(sDB); err = Application.GetLastError(); if (err == SQLite.ROW) then tbRow = SQLite.GetRowDataAssoc(db); for strColumn, strData in tbRow do if String.Find(strData, sTable, 1, false) > -1 then lenData = String.Length(strData); nPos = 1; nFieldCounter = 0; while nPos < lenData do nPosDel = String.Find(strData, ", ", nPos, false); if nPosDel > -1 then nFieldCounter = nFieldCounter + 1; nPos = nPosDel + 2; else break; end end nFieldCounter = nFieldCounter + 1; end end end until (err ~= SQLite.ROW) SQLite.Finalize(db) return nFieldCounter; end

