diff --git a/PostgreSQL/postgresql.py b/PostgreSQL/postgresql.py index 76c9c006b..1fd537b56 100644 --- a/PostgreSQL/postgresql.py +++ b/PostgreSQL/postgresql.py @@ -190,6 +190,23 @@ def table_exists(self, table): "WHERE table_name=%s;", [table]) return self.fetchone()[0] != 0 + def column_exists(self, table, column): + """ + Test whether the specified SQL column exists in the specified table. + :param table: table name to check. + :type table: str + :param column: column name to check. + :type column: str + :returns: True if the column exists, False otherwise. + :rtype: bool + """ + self.__cursor.execute( + "SELECT COUNT(*) FROM information_schema.columns " + "WHERE table_name = %s AND column_name = %s", + (table, column) + ) + return self.fetchone()[0] != 0 + def close(self): self.__connection.close()