'Return a list of (column_name, referenced_table_name, referenced_column_name)
for all key columns in given table.'

def get_key_columns(self, cursor, table_name):

    key_columns = []
    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
        key_columns.append(tuple((s.strip('"') for s in m.groups())))
    return key_columns
