Dump column values

This commit is contained in:
Peter J. Holzer 2022-11-22 11:46:30 +01:00
parent db1c418ee5
commit 1fd1eff893
1 changed files with 34 additions and 0 deletions

34
table_info Executable file
View File

@ -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)