0

How can I print data in table format with headers? Like in the photo I posted hereenter image description here

Is it even possible? My code here:

    conn = sql.connect("register.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM datas ORDER BY lastname")
    record = cur.fetchall()
    for item in record:
        print(item[0],"  ",item[1],"  ",item[2],"  ",item[3],"  ",item[4])
    print("Wpisz nr id: ")
    conn.commit()
    conn.close()
3
  • 3
    Does this answer your question? Python - How to print Sqlite table, have you tried to print(record) ? Commented Aug 16, 2020 at 12:55
  • No this isn't this answer I can print out data from SQLite3 but not in format table that's every column and line is even with headers Commented Aug 16, 2020 at 12:56
  • The question is how to print in pretty format like in the photo Commented Aug 16, 2020 at 12:57

1 Answer 1

1

You could use a the sqlite row_factory Row to get access to the column names (to be used as titles) using the keys method. Then use something like the string method ljust in the print statment, supplying the desired widths to get the formatting you desire.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.