Python Forum

Full Version: Extract SQL Query Results in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
rather than using fetchall, use:

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

Thanks
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?