Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Format SQL Query Output
#1
I'm selecting data from a table and returning all records. However, the records are being returned with a blank space separator instead of a comma. Any idea how to return rows like the following:

Output:
1,sales,marketing,04212020
Instead of:

Output:
1 sales marketing 04212020
Here's the function:

def select_from_database(conn, tablename):
    cursor = conn.cursor()
    print("Connected to: %s" % database)

    cursor.execute("SELECT * FROM %s " % tablename)
    rows = cursor.fetchall()
    print("Total records: %s" % len(rows))

    for i in rows:
        print(*i)
        #print("%s %s" % (row[0], row[1]))

    cursor.close()
Thanks!
Reply


Messages In This Thread
Format SQL Query Output - by phillyfa - Apr-21-2020, 07:23 PM
RE: Format SQL Query Output - by Larz60+ - Apr-21-2020, 08:06 PM
RE: Format SQL Query Output - by buran - Apr-22-2020, 07:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Query output in Tuple paulo79 2 2,034 Apr-07-2022, 12:54 PM
Last Post: paulo79
  format the output from a nested dictionary. nostradamus64 9 4,717 May-03-2021, 04:45 PM
Last Post: nostradamus64
  JupyterLab Dictionary Content Output Format Ourkid123uk 0 1,347 Sep-04-2020, 02:18 PM
Last Post: Ourkid123uk
  MYSQL Update Query format simdo01 1 2,272 Aug-31-2020, 12:59 AM
Last Post: nilamo
  str.endswith Output Query eddywinch82 8 3,939 Aug-21-2020, 11:59 PM
Last Post: eddywinch82
  Save output into a Excel Sheet with Format Table skaailet 1 2,534 Apr-17-2020, 11:56 PM
Last Post: thirteendec
  Display output in readable format and save hnkrish 1 2,685 Jul-19-2019, 09:29 AM
Last Post: Larz60+
  About the output format. shang2019 4 2,793 Jan-10-2019, 07:38 PM
Last Post: buran
  python script to get wildcard mask output in the following format techrichit 0 3,861 Aug-10-2018, 11:01 PM
Last Post: techrichit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020