在python中看不到数据库查询输出

发布于 2024-10-17 09:09:51 字数 445 浏览 1 评论 0原文

我正在从 python 执行一个简单的 mssql 查询。 我可以在探查器中看到查询到达数据库。 该查询有 1 行答案。 我无法在 Python shell 中看到输出

我运行下面的代码

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']) 

请告知 谢谢, 阿萨夫

I'm execute a simple mssql query from python.
I can see in the profiler that the query reach the DB.
The query has 1 row of answer.
I fail to see the output in the 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']) 

Pleas advise
Thanks,
Assaf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-10-24 09:09:51

您可以在执行后调用 fetchone() 或 fetchall() 从该查询中获取数据。

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()

You can call fetchone() or fetchall() after execute to get the data from that 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()
诗笺 2024-10-24 09:09:51
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'])

尝试将结果分配给某些内容而不是使用光标。

cur.execute() 是一个函数,因此虽然它确实返回一个值(您看到的),但您没有将其分配给任何东西,因此当您执行 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()   

此版本对 cur.execute() 的结果执行 for 循环>,但我真的建议不要这样做

(小附录:我忘记了 fetchall 的,我习惯把它放在一个函数中。抱歉)

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 the results to something instead of using the cursor.

cur.execute() is a function, as such while it does return a value (which you saw), you're not assigning it to anything, so when you go to do the for loop, there's nothing to loop over.

If you don't want to store the result, you could do this (rather messy) version:

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()   

This one does the for loop over the result of the cur.execute(), but I really advise against this

(Minor addendum: I forgot the fetchall's, I'm so used to putting this in a function. Sorry)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文