list - Comparing tuple elements against integers with Python -


i having hard time converting data. select data database, returned in tuple format. try convert them using list(), list of tuples. trying compare them integers receive parsing json. easiest way convert , compare these two?

from dbconnection import db  import pymssql data import jsonparse  db.execute('select id party partyid = 1') parse = jsonparse.parse()  row in cursor:     curlist = list(cursor)  = 0   testdata in parse:     print curlist[i], testdata['data']     += 1 

output:

(6042,) 6042  (6043,) 6043  (6044,) 6044  (6045,) 6045 

sql results come rows, sequences of columns; true if there 1 column in each row.

next, executing query on db object (whatever may be), iterating on cursor; if works @ more down luck. you'd execute query on cursor object.

if expect 1 row returned, can use cursor.fetchone() retrieve 1 row. for row in cursor loop skipping first row.

you use:

cursor = connection.cursor() cursor.execute('select id party partyid = 1') result = cursor.fetchone()[0] 

to retrieve first column of first row, or use tuple assignment:

cursor = connection.cursor() cursor.execute('select id party partyid = 1') result, = cursor.fetchone() 

if need match against multiple rows, use list comprehension extract id columns:

cursor = connection.cursor() cursor.execute('select id party partyid = 1') result = [row[0] row in cursor] 

now have list of id values.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -