Python Forum
results for sql server query not as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
results for sql server query not as expected
#1
what am I doing wrong and what is with all the letters u??
for row in cursor.execute('select top 1 *  from  server.dbo.SalesOrders left join  server.dbo.SalesOrderLines on omlSalesOrderID=ompSalesOrderID '):
   print(row)
results:

(u'10000', u'', u'', u'10009', u'', u'', u'', u'', u'10009', u'1', u'', u'8192011', datetime.datetime(2011, 8, 19, 0, 0), datetime.datetime(2011, 8, 19, 0, 0), u'SHIPPING POINT', u'KITH', u'', u'2TN30', u'', u'', u'', None, None, False, False, u'', u'', False, Decimal('1.000000'), Decimal('2991.80'), Decimal('2991.80'), Decimal('0.00'), Decimal('0.00'), Decimal('2991.80'), Decimal('2991.80'), Decimal('0.00'), Decimal('0.00'), Decimal('0.00000'), Decimal('299.18'), Decimal('299.18'), Decimal('299.18'), Decimal('299.18'), u'', Decimal('0.00'), Decimal('0.00'), u'', Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), Decimal('3290.98'), Decimal('3290.98'), 3, None, None, u'', u'', True, datetime.datetime(2011, 9, 6, 0, 0), False, Decimal('0.00'), False, Decimal('0.00'), Decimal('0.00'), u'', False, u'KIM', datetime.datetime(2011, 8, 19, 10, 23, 52), '04EBA251-1493-486C-B1E0-A4FCDABCE86C', u'', u'', False, False, u'', u'', u'', u'', u'', u'', u'', 0, None, None, False, Decimal('0.00'), Decimal('299.18'), False, None, u'', u'', u'', u'NON', u'', False, Decimal('0.00'), False, Decimal('0.00'), u'', 5, Decimal('0.00'), u'', u'', False, False, None, Decimal('0.00'), None, Decimal('0.00'), Decimal('0.00'), u'0', u'', u'', False, u'', Decimal('0.00'), datetime.datetime(2011, 9, 2, 0, 0), False, Decimal('0.00'), u'', False, Decimal('0.00'), False, 0, False, False, False, u'', False, u'10000', u'', False, False, False, u'', u'', 0, False, u'', False, False, u'', Decimal('0.00'), Decimal('0.00'), Decimal('43'), Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), u'F0006', Decimal('423.74'), Decimal('0.00000'), Decimal('0.00'), Decimal('0.00'), Decimal('100.00'), u'', False, u'10000', 1, u'110-39', u'', u'', u'EA', u'110', u'Twin Bookcase HB -Tanner -Brazilian Cherry', u'', None, None, Decimal('2.00000'), Decimal('52.00000'), Decimal('52.00000'), Decimal('0.00'), Decimal('0.00000'), Decimal('0.00000'), Decimal('52.00000'), Decimal('52.00000'), Decimal('104.00'), Decimal('104.00'), Decimal('0.00'), Decimal('0.00'), Decimal('104.00'), Decimal('104.00'), Decimal('0.00'), Decimal('0.00'), u'', u'RESAL', Decimal('0.00'), Decimal('0.00'), u'', Decimal('0.00'), Decimal('0.00'), True, False, Decimal('2.00000'), u'', 0, 0, u'', 0, u'', 0, False, u'', u'', Decimal('0.00000'), u'', u'', 0, True, False, False, Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), False, False, None, False, u'KIM', datetime.datetime(2011, 8, 19, 10, 28, 21), '415BC3CD-BB7A-47DB-987A-C7E8CF2815E0', Decimal('0.00'), u'', Decimal('5.36'), Decimal('2'), u'', Decimal('0.000'), u'', u'', False, u'', False, False, False, Decimal('0.00'), u'', u'', u'', u'', Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), Decimal('0.00'), u'', u'', Decimal('0.00'), Decimal('0.00000'), Decimal('10.72'), Decimal('0.00000'), Decimal('2.00000'), u'', u'')
Reply
#2
prefix u in Python 2 indicates unicode string when you print an object containing string - you will not see if you print an individual string.

The result is returned as a tuple - what did you expect?
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
This is my first query using python.
how would I drop the "u" and "decimal" from the printed results?
also is correct way to print the results? It does seem to put me in an endless loop.
for row in cursor.execute('select top 1 ompSalesOrderID, ompfullorderSubtotalBase  from server.dbo.SalesOrders left join  server.dbo.SalesOrderLines on omlSalesOrderID=ompSalesOrderID '):
    while row:
        print ((row[0] , row[1]) )
Thanks for the Guidance
Reply
#4
(Sep-13-2018, 03:25 PM)kat35601 Wrote: This is my first query using python.
how would I drop the "u" and "decimal" from the printed results?
also is correct way to print the results? It does seem to put me in an endless loop.
for row in cursor.execute('select top 1 ompSalesOrderID, ompfullorderSubtotalBase  from server.dbo.SalesOrders left join  server.dbo.SalesOrderLines on omlSalesOrderID=ompSalesOrderID '):
    while row:
        print ((row[0] , row[1]) )
Thanks for the Guidance

Try
for row in cursor.execute('...'):
    for cell in row:
        print(cell, sep=',')
If you know indices of the "interesting" fields - and they are few, e.g. elements 2, 6 and 1 - you can print like this
print('{2}, {6}, {1}'.format(*row))
(see this page for explanation)

Since row in your example does not change - remains non-empty tuple, which in Python is one of True equivalents - endless loop is the expected behaviour.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
That works ...Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Method works as expected on host machine but not on server gradlon93 4 1,095 Jan-05-2023, 10:41 AM
Last Post: DeaD_EyE
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,146 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,138 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  How to take the tar backup files form remote server to local server sivareddy 0 1,917 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,246 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 3,180 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Error Handling/No results from SQL Query JP_ROMANO 7 9,512 Jul-18-2018, 02:31 PM
Last Post: JP_ROMANO
  Greenplum Query results to Email HTML table krux_rap 1 2,518 Apr-10-2018, 09:12 PM
Last Post: Larz60+
  Extract SQL Query Results in Python boobalganesan 3 11,807 Jan-11-2018, 07:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

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