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
#2
I'd like to suggest sqlalchemy as a great alternative.
It does a lot of background tasks for you automatically.
Basics are simple to learn.
I have a tutorial on this forum here: https://python-forum.io/Thread-SqlAlchem...-Data-Load.
In a nutshell, you build a model which includes a class for each table, and import that model whenever a project needs access to your database.
Whenever you make an update (like adding a new column), the model will create the column for you (if you wish) and every module where that table is used will immediately have access (through the model) to the new data in that column.
Reply
#3
rows is list of tuples. By print(*i) you just unpack each tuple into print and the default separator is space.
The simplest solutions is to specify sep argument to print function
print(*i, sep=',')
There are other options like using str.join() or string formatting. These would be helpful if you want to construct a string and assign it to a name.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Query output in Tuple paulo79 2 1,968 Apr-07-2022, 12:54 PM
Last Post: paulo79
  format the output from a nested dictionary. nostradamus64 9 4,530 May-03-2021, 04:45 PM
Last Post: nostradamus64
  JupyterLab Dictionary Content Output Format Ourkid123uk 0 1,308 Sep-04-2020, 02:18 PM
Last Post: Ourkid123uk
  MYSQL Update Query format simdo01 1 2,216 Aug-31-2020, 12:59 AM
Last Post: nilamo
  str.endswith Output Query eddywinch82 8 3,815 Aug-21-2020, 11:59 PM
Last Post: eddywinch82
  Save output into a Excel Sheet with Format Table skaailet 1 2,495 Apr-17-2020, 11:56 PM
Last Post: thirteendec
  Display output in readable format and save hnkrish 1 2,624 Jul-19-2019, 09:29 AM
Last Post: Larz60+
  About the output format. shang2019 4 2,690 Jan-10-2019, 07:38 PM
Last Post: buran
  python script to get wildcard mask output in the following format techrichit 0 3,821 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