Python Forum
Extract SQL Query Results in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract SQL Query Results in Python
#1
How to extract query results of an SQL in a comma delimited string variable in Python

conn = ora.connect("HR", "oracle", "localhost/xe")
cur=conn.cursor()
cur.execute('select * from employees')
cur.fetchall()

The above is the part of my code. Now, how can I load these results into a string variable which has comma separated values from my employees table?

Thank you,
B
Reply
#2
rather than using fetchall, use:

for row in cur:
    print(row)
Reply
#3
I knew this but how can I save them in a string variable instead of printing them?

Thanks
Reply
#4
the cursor will return list of tuples, so something like
for row in cur:
    empl_data = ','.join(row)
    # do something with empl_data
however, what you want to do with this string of comma-separated values? write to a csv file?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list the files using query in python arjunaram 0 670 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  python sql query single quote in a string mg24 1 1,054 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  python extract mg24 1 946 Nov-02-2022, 06:30 PM
Last Post: Larz60+
  "SUMIF" type query in Python (help required) BlainEillimatta 0 851 Oct-06-2022, 09:08 AM
Last Post: BlainEillimatta
  MSSQL query not working in Python kat35601 0 913 Apr-12-2022, 06:44 PM
Last Post: kat35601
  Error using mariadb select query with form in python? shams 2 2,003 Jul-29-2021, 12:30 PM
Last Post: shams
  How to save some results in .txt file with Python? Melcu54 4 7,322 May-26-2021, 08:15 AM
Last Post: snippsat
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,233 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Python mysql query help please tduckman 4 4,310 Mar-13-2020, 03:42 PM
Last Post: Marbelous
  How to append one function1 results to function2 results SriRajesh 5 3,145 Jan-02-2020, 12:11 PM
Last Post: Killertjuh

Forum Jump:

User Panel Messages

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