def putkeys():
    cursor.execute("SELECT id, dn FROM thunderpeople")
    people = cursor.fetchall()

    for person in people:
        person_id = person[0]
        person_dn = person[1]

        # Handle email addresses with pesky quotes.
        person_dn = person_dn.replace('"', r'\"') 

        query = 'UPDATE thundergroup SET thunderpeople_id' \
        ' = "%s" WHERE member = "%s"' % (person_id, person_dn)
        print query
        cursor.execute(query)


    query = 'SELECT * FROM thundergroup WHERE thunderpeople_id' \
    ' IS NULL;'
    cursor.execute(query)
    result = cursor.fetchall()
    if result:
        try:
            raise Exception('F*ck thunder bird')
        except Exception:
            print "thundergroup records with no foreign key:"
            for record in result:
                print record
            raise
Example #2
0
cursor.execute(create_info)

get_columns = """
SELECT column_name FROM COLUMNS
WHERE table_schema = '%s'
AND table_name = 'master'
""" % settings.DATABASE
info_c.execute(get_columns)

columns = info_c.fetchall()

columns = tuple(map(lambda col: col[0], columns ))

cursor.execute("SELECT distinct(source) FROM master")

sources = tuple(map(lambda res: res[0], cursor.fetchall()))

do_tally_base = r"""
SELECT * FROM
(SELECT %s as sb1, COUNT(*) as sb2, source as sb3 FROM master
WHERE source = '%s'
AND %s IS NOT NULL
GROUP BY %s ORDER BY COUNT(*) DESC) as t2
WHERE sb2 > 1
"""

for source in sources:
    for column in columns:
        do_tally = do_tally_base % (column, source, column, column)
        cursor.execute(do_tally)
        stuff = cursor.fetchall()