Can't see DB query output in python

I am executing a simple mssql query from python. I see in the profiler that the request reaches the database. The request has 1 response line. I do not see output in Python shell

I run the code below

import pymssql 
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True) 
cur = conn.cursor()  
cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe') 
for row in cur:     
    print "ID=%d, Name=%s" % (row['id'], row['name']) 

Please inform Thanks, Assaf

+3
source share
2 answers

You can call fetchone () or fetchall () after execution to get data from this query.

import pymssql 
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True) 
cur = conn.cursor()  
cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe') 
print cur.fetchall()
+3
source
import pymssql  
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True)  
cur = conn.cursor()   
users = cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe').fetchall()  
conn.close()   

for row in users:     
    print "ID=%d, Name=%s" % (row['id'], row['name'])

Try assigning results to something instead of using the cursor.

cur.execute()is a function as such, while it returns (which you see), you can not assign it to anything, so when you go to do the cycle for,.

, ( ) :

import pymssql  
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True)  
cur = conn.cursor() 
sql = 'SELECT * FROM persons WHERE salesrep=%s'

for row in cur.execute(sql, 'John Doe').fetchall():
    print "ID=%d, Name=%s" % (row['id'], row['name'])

conn.close()   

for cur.execute(),

( : fetchall, . )

+1

Source: https://habr.com/ru/post/1792238/


All Articles