How to get a list of column names with Sqlite.swift?

Solutions Collect From Internet About “How to get a list of column names with Sqlite.swift?”

Assuming you already have a database connection called db set up, to get a list of the column names, you can use the following code:

do {

    let tableInfo = Array(try db.prepare("PRAGMA table_info(table_name)"))
    for line in tableInfo {
        print(line[1]!, terminator: " ")

} catch _ { }

where table_name is replaced with the literal string of your table’s name.

You can also add


to see more pragma info about your table.


Thanks to this answer for clues of how to do this.

Example Function

Tested routine from Joe Blow to save a little typing:

func findColumns(_ tableName:String) {

    var asAnArray:[String] = []
    do {
        let s = try db!.prepare("PRAGMA table_info(" + tableName + ")" )
        for row in s { asAnArray.append(row[1]! as! String) }
    catch { print("some woe in findColumns for \(tableName) \(error)") }

    let asAString = asAnArray.joined(separator: ",")