2017-01-23 39 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()來遍歷所有行,即使我們不關心結果。