2017-01-23 64 views
0

此主题已被涵盖。如果是这样,为此道歉。从数据库中获取行(使用“for”和“while”循环)并从控制台执行脚本时遇到问题。Py:从数据库提取并从控制台执行脚本时限制

我需要从数据库中获取大量的行,我正在构建一个脚本,以便我可以插入用户标识,并且我将获取客户端和状态的帐户标识。 我已经意识到,当我从Eclipse运行脚本时,完整的输出从DB中获取。当我从控制台运行脚本时,行数有限制。所以,我想知道如果我有一个“while行不是无”循环...为什么我的行变成无如果数据库中有更多的行?

另外:我需要解决这个问题。不管怎样。我宁愿不将完整列表加载到本地文件(如果可能)。但是,如果没有其他方式......好吧,请帮助!这是我的例子。

#!/usr/bin/env python3 
# encoding: utf-8 

import configparser 
import pymysql 
from prettytable import PrettyTable 

conn = pymysql.connect(host=host, user=user, passwd=password, db=database) 

print() 
userid = input('Insert User ID(s) > ') # userid is a list of 2000 users in comma-separated format 
userids = userid.replace(" ", "").replace("'", "").replace(",", "','") 
cur = conn.cursor() 
cur.execute(""" SELECT user_ID, account_ID, status FROM Account WHERE user_ID IN ('%s'); """ % userids) 
rows = cur.fetchall() 

table = PrettyTable(["user_ID", "account_ID", "status"]) 
table.padding_width = 1 

for line in rows: 
    user_ID = str(line[0]) 
    account_ID = str(line[1]) 
    status = str(line[2]) 
    table.add_row([user_ID, account_ID, status]) 

print() 
print(table) 
conn.close() 
print() 
print() 
exit() 

回答

0

不修改,你可以尝试使用由prettytable模块像这样提供from_db_cursor()太多代码:

#!/usr/bin/env python3 

import pymysql 
from prettytable import from_db_cursor 

conn = pymysql.connect(host=host, user=user, passwd=password, db=database) 

userid = input('Insert User ID(s) > ') # userid is a list of 2000 users in comma-separated format 
userids = userid.replace(" ", "").replace("'", "").replace(",", "','") 
cur = conn.cursor() 
cur.execute(""" SELECT user_ID, account_ID, status FROM Account WHERE user_ID IN ('%s'); """ % userids) 

table = from_db_cursor(cur, padding_width=1) 

print(table) 
conn.close() 

但看prettytable我猜测的源代码,它不会改变这种情况很多,因为它或多或少地做了你在代码中明确做的事情。


什么可能会更好地工作,是一次添加行之一table,而不是获取所有行,并通过他们循环,从而增加他们。喜欢的东西:

#!/usr/bin/env python3 

import pymysql 
from prettytable import PrettyTable 

conn = pymysql.connect(host=host, user=user, passwd=password, db=database) 

userid = input('Insert User ID(s) > ') # userid is a list of 2000 users in comma-separated format 
userids = userid.replace(" ", "").replace("'", "").replace(",", "','") 
cur = conn.cursor() 
cur.execute(""" SELECT user_ID, account_ID, status FROM Account WHERE user_ID IN ('%s'); """ % userids) 
row = cur.fetchone() 

table = PrettyTable(["user_ID", "account_ID", "status"], padding_width=1) 

while row is not None: 
    table.add_row(row) 
    row = cur.fetchone() 

print(table) 
conn.close() 

你不需要因为Prettytable行元素转换为字符串值确实在内部。


但它可以利用不同的功能来简化你的代码,并使其更Python:

#!/usr/bin/env python3 

import re 
import pymysql 
from prettytable import PrettyTable 

userid = input('Insert User ID(s) > ') # userid is a list of 2000 users in comma-separated format 
userids = re.sub("[ ']", "", userid).replace(",", "','") 

table = PrettyTable(["user_ID", "account_ID", "status"], padding_width=1) 

with pymysql.connect(host=host, user=user, passwd=password, db=database) as cur: 
    cur.execute("""SELECT user_ID, account_ID, status FROM Account WHERE user_ID IN ('%s'); """ % userids) 

    map(table.add_row, cur) 

print(table) 

在这个版本:

  • 我已经使用了re.sub()做一些替换(有些人可能会说这有点矫枉过正,但它可能对你将来有用)
  • A Pymysql连接implementscontext manager直接为您提供游标。
  • 由于Pymysql游标可以provideiterator我们可以使用它与map()来遍历所有行,即使我们不关心结果。