Hi, thanks for developing SQLite.swift!
I want to add columns from code, and would like to check if column already exists. How can I do it?
Thanks!!!
This is how I solved it:
````
extension Connection {
public func exists(column: String, in table: String) throws -> Bool {
let stmt = try prepare("PRAGMA table_info((table))")
let columnNames = stmt.makeIterator().map { (row) -> String in
return row[1] as? String ?? ""
}
return columnNames.contains(where: { dbColumn -> Bool in
return dbColumn.caseInsensitiveCompare(column) == ComparisonResult.orderedSame
})
}
}
````
This is how I solved it:
extension Connection { public func exists(column: String, in table: String) throws -> Bool { let stmt = try prepare("PRAGMA table_info(\(table))") let columnNames = stmt.makeIterator().map { (row) -> String in return row[1] as? String ?? "" } return columnNames.contains(where: { dbColumn -> Bool in return dbColumn.caseInsensitiveCompare(column) == ComparisonResult.orderedSame }) } }
thx) and how check record is already exist? (i want know i must update or insert)
Most helpful comment
This is how I solved it:
````
extension Connection {
public func exists(column: String, in table: String) throws -> Bool {
let stmt = try prepare("PRAGMA table_info((table))")
}
````