简体   繁体   English

将 sql 存储在 python 中的变量中

[英]store sql result in a variable in python

I need help in storing the query result in a variable after executing the select statement.在执行 select 语句后,我需要帮助将查询结果存储在变量中。 this is my code below.. there are no error but i need help in storing it in a variable in PYTHON. i am using pymssql and python 2.7.9.这是我下面的代码。没有错误,但我需要帮助将其存储在 PYTHON 中的变量中。我正在使用 pymssql 和 python 2.7.9。 thanks in advance提前致谢

conn = pymssql.connect(host='localhost', user='xx', password='xx', database='dbpython')
    c = conn.cursor()
    row = c.execute("select fid, fname, lname, contact from tblcontact where fname='maiji'")
    row = c.fetchall()
    while row:
        print row
        row = c.fetchall()

Stripping down your code, this is what you're doing in short.简而言之,这就是您正在做的事情。

row = c.ex...
row = c.fe..
    row = c.fe...

In programming, this is called assigning a value to a variable.在编程中,这称为给变量赋值。 In short, row is the container and you're replacing it over and over multiple times.简而言之,行是容器,您要多次替换它。

We'll ignore optmizations for now and just go with the basics.我们暂时忽略优化,只忽略 go 的基础知识。

conn = pymssql.connect(host='localhost', user='xx', password='xx', database='dbpython')
c = conn.cursor()
row = c.execute("select fid, fname, lname, contact from tblcontact where fname='maiji'")
rows = c.fetchall()
for row in rows:
    print(row)

First of all, c.fetchall() retrieves ALL the results from your query, we'll put them in a variable called rows .首先, c.fetchall()从您的查询中检索所有结果,我们将把它们放在一个名为rows的变量中。 Then we create a iterator (the thing you tried to do with the while loop) by doing for row in rows .然后我们通过执行for row in rows创建一个迭代器(你试图用 while 循环做的事情)。 Then we simply print each row.然后我们简单地打印每一行。

How you should do it tho你应该怎么做

This is called context managers.这称为上下文管理器。

with pymssql.connect(host='localhost', user='xx', password='xx', database='dbpython') as conn:
    c = conn.cursor()
    rows = c.execute("select fid, fname, lname, contact from tblcontact where fname='maiji'")
    for row in c.fetchall():
        print(row)

This opens a context handle called conn which will automatically close the connection when you're done with your code (leaving no open connections which could be a problem) and secondly it's less code iterating directly over c.fetchall() and potentially be faster if the function supports iterations, which it probably will in later versions.这将打开一个名为conn的上下文句柄,它会在您完成代码时自动关闭连接(不留下任何打开的连接,这可能是一个问题),其次,直接迭代c.fetchall()的代码更少,如果可能会更快function 支持迭代,它可能会在以后的版本中支持。


Here's one where you just add upp all the results into a hughe string (for dumping out to a file etc for instance).在这里,您只需将所有结果添加到一个 hughe 字符串中(例如,用于转储到文件等)。 Note that this might fill your memory if the results are many, sequentially storing this to the file in such a case would be better, hence why you normally work with each database row individually for memory reasons.请注意,如果结果很多,这可能会填满您的 memory,在这种情况下将其顺序存储到文件中会更好,因此您通常出于 memory 的原因单独处理每个数据库行。

with pymssql.connect(host='localhost', user='xx', password='xx', database='dbpython') as conn:
    c = conn.cursor()
    rows = c.execute("select fid, fname, lname, contact from tblcontact where fname='maiji'")
    string_result = ''
    for row in c.fetchall():
        string_result = row + '\n'

You should use an iterator instead of while loop.您应该使用迭代器而不是 while 循环。

conn = pymssql.connect(host='localhost', user='xx', password='xx', database='dbpython')
c = conn.cursor()
c.execute("select fid, fname, lname, contact from tblcontact where fname='maiji'")

for row in c:
    print(row)

c.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM