Python Forum

Full Version: results for sql server query not as expected
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'')
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?
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
(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.
That works ...Thanks