diff --git a/table_info b/table_info new file mode 100755 index 0000000..a2c55c7 --- /dev/null +++ b/table_info @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +import argparse + +import MySQLdb + +ap = argparse.ArgumentParser() +ap.add_argument("--host") +ap.add_argument("--dbname") +ap.add_argument("--user") +ap.add_argument("--password") +ap.add_argument("table") + +args = ap.parse_args() + +db = MySQLdb.connect(host=args.host, database=args.dbname, user=args.user, password=args.password) +csr = db.cursor() + +csr.execute(f"select * from {args.table}") +values = { x[0]: {} for x in csr.description} +for r in csr: + for i in range(len(r)): + c = csr.description[i][0] + v = r[i] + if v not in values[c]: + values[c][v] = 0 + values[c][v] += 1 + +for c in values.keys(): + n_values = len(values[c].keys()) + n_unique = len([x for x in values[c].values() if x == 1]) + print ("Column", c, n_values, "values", n_unique, "unique") + for v, count in values[c].items(): + print("", v, count)