4 ms·
I answered a question once with a rather simple O(n) solution: https://stackoverflow.com/questions/10865483/print-results-in-mysql-format-with-python/20383011#
by dotancohen 2y ago
I answered a question once with a rather simple O(n) solution:
https://stackoverflow.com/questions/10865483/print-results-in-mysql-format-with-python/20383011#20383011 https://stackoverflow.com/questions/10865483/print-results-i...
sql = "SELECT * FROM someTable"
cursor.execute(sql)
conn.commit()
results = cursor.fetchall()
widths = []
columns = []
tavnit = '|'
separator = '+'
for cd in cursor.description:
widths.append(max(cd[2], len(cd[0])))
columns.append(cd[0])
for w in widths:
tavnit += " %-"+"%ss |" % (w,)
separator += '-'*w + '--+'
print(separator)
print(tavnit % tuple(columns))
print(separator)
for row in results:
print(tavnit % row)
print(separator)
Am I missing anything, are there any glaring bugs? I don't see this as a particularly difficult problem.