From 1fd1eff893f5051586dfcce87a8c131a0e20552b Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Tue, 22 Nov 2022 11:46:30 +0100 Subject: [PATCH] Dump column values --- table_info | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 table_info 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)