Value of type 'Any' has no member 'int'. Unable to fetch count of rows from PostgresClientKit

Advertisements

I am trying to fetch the count of rows from the PostgreSQL database using PostgresClientKit but I am getting the following error
Value of type ‘Any’ has no member ‘int’.

This is the code

let rows = fetchSQL(statement: "SELECT count(*) FROM ag_graph WHERE name='" + graphName + "'", connection: connection);
        
        let row_counts = rows[0] as! [Any];
        let count = try row_counts[0].int();
        if count < 1{
            // My Code
        }

I have tried printing the rows. It is

[[1]]

The definition of fetchSQL function is as follow

func fetchSQL(statement:String, connection:Connection)-> [Any]{
    var rows:[Any] = [];
    do{
        let statement = try connection.prepareStatement(text: statement)
        defer { statement.close() }
        
        let cursor = try statement.execute()
        defer { cursor.close() }

        for row in cursor {
            rows.append(try row.get().columns);
        }
    }catch{
        print(error)
    }
    
    return rows;
}

>Solution :

Try this way . It will probably help.

 let rows = fetchSQL(statement: "SELECT count(*) FROM ag_graph WHERE name='" + graphName + "'", connection: connection);
            print(rows)
            let row_counts = rows[0] as! [PostgresValue];
            let count = try row_counts[0].int();
            if count < 1{
                // your code
            }

Leave a ReplyCancel reply