if len(queryArgs) == 0:
            queryArgs = None

        queryStr = sql.SQL('''
            SELECT * FROM {id_anno} AS anno
            JOIN (SELECT id AS imID, filename FROM {id_img}) AS img
            ON anno.image = img.imID
            {sql_limitUsers}
            {sql_excludeUsers}
        ''').format(id_anno=sql.Identifier(args.project, 'annotation'),
                    id_img=sql.Identifier(args.project, 'image'),
                    sql_limitUsers=sql_limitUsers,
                    sql_excludeUsers=sql_excludeUsers)

        cursor = dbConn.execute_cursor(queryStr, queryArgs)

        # iterate
        print('Querying database...\n')
        while True:
            nextItem = cursor.fetchone()
            if nextItem is None:
                break

            # parse
            if nextItem['label'] is None:
                # TODO: it might happen that an annotation has no label; skip in this case
                continue

            imgName = nextItem['filename']
            label = labeldef[nextItem['label']][1]  # store label index
            queryArgs.append(tuple(limitUsers))

        if args.exclude_users is not None:
            excludeUsers = []
            for u in args.exclude_users.split(','):
                excludeUsers.append(u.strip())
            if args.limit_users is not None:
                sql += 'AND anno.username NOT in %s'
            else:
                sql += 'WHERE anno.username IN %s'
            queryArgs.append(tuple(excludeUsers))

        if len(queryArgs) == 0:
            queryArgs = None

        cursor = dbConn.execute_cursor(sql, queryArgs)


        # iterate
        print('Exporting images...\n')
        while True:
            nextItem = cursor.fetchone()
            if nextItem is None:
                break
        
            # parse
            imgName = nextItem['filename']
            imgName, _ = os.path.splitext(imgName)
            targetName = os.path.join(args.target_folder, imgName+'.'+args.file_format)
            parent,_ = os.path.split(targetName)
            os.makedirs(parent, exist_ok=True)