'Returns a dictionary of {field_index: (field_index_other_table, other_table)}
representing all relationships to the given table. Indexes are 0-based.'

def get_relations(self, cursor, table_name):

    relations = {}
    cursor.execute('SELECT    sql    FROM    sqlite_master    WHERE    tbl_name    =    %s    AND    type    =    %s', [table_name, 'table'])
    results = cursor.fetchone()[0].strip()
    results = results[(results.index('(') + 1):results.rindex(')')]
    for (field_index, field_desc) in enumerate(results.split(',')):
        field_desc = field_desc.strip()
        if field_desc.startswith('UNIQUE'):
            continue
        m = re.search('references    (.*)    \\(["|](.*)["|]\\)', field_desc, re.I)
        if (not m):
            continue
        (table, column) = [s.strip('"') for s in m.groups()]
        cursor.execute('SELECT    sql    FROM    sqlite_master    WHERE    tbl_name    =    %s', [table])
        result = cursor.fetchone()
        if (not result):
            continue
        other_table_results = result[0].strip()
        (li, ri) = (other_table_results.index('('), other_table_results.rindex(')'))
        other_table_results = other_table_results[(li + 1):ri]
        for (other_index, other_desc) in enumerate(other_table_results.split(',')):
            other_desc = other_desc.strip()
            if other_desc.startswith('UNIQUE'):
                continue
            name = other_desc.split('    ', 1)[0].strip('"')
            if (name == column):
                relations[field_index] = (other_index, table)
                break
    return relations
