如何在Python中将所有sqlite3数据提取到漂亮的打印中
我是 python 新手,到目前为止我需要有关 sqlite3 输出的帮助
(u'GABRIELLA TEGGIA (40)', u'-', u'65')
(u'PUTRA', u'DPS', u'11')
(u'SUASTINI', u'-', u'-')
(u'ARI UTAMA', u'JL.D.BERATAN GG.8/4', u'26')
(u'SUYASA', u'DS.TUNJUK,TABANAN', u'33')
(u'SARAH(41)', u'BEJI AYU', u'40')
(u'AA.GDE OKA', u'JL.H.WURUK 179 DPS', u'53')
(u'AYU NILA RADIASTUTI', u'ABIAN BASE,KAPAL', u'8')
(u'DANTI I NYOMAN', u'GIANYAR', u'50')
(u'INDAYANI', u'BUNUTIN,UBUD', u'6')
(u'JENNI', u'JL.MUTIARA NO.18 KEROBOKAN', u'37')
(u'JERRY G.J ABELL', u'HOLLAND', u'-')
(u'COK ALIT', u'BR.MUKTI,SINGAPADU', u'35')
(u'SUPARTI NI PUTU', u'KARANGASEM', u'51')
(u'SUMADA I GD', u'SINGARAJA', u'60')
,但我只能获取最后一行,就像这样
+-------------+-----------+------+
| Pasien | Alamat | Umur |
+-------------+-----------+------+
| SUMADA I GD | SINGARAJA | 60 |
+-------------+-----------+------+
如何获取要打印的完整行? 我在这里提供我的代码:
import sqlite3
connection = sqlite3.connect('data.db3')
cursor=connection.cursor()
print("Show data row by row:")
print('-'*40)
from prettytable import PrettyTable
x = PrettyTable()
x.set_field_names(["Pasien", "Alamat", "Umur"])
cursor.execute('SELECT nm_pasien, almt_pasien, umur_pasien from pasien limit 15')
for nm_pasien in cursor:
print nm_pasien
print('-'*40)
x.add_row(nm_pasien)
x.printt()
我正在使用 这里 的 python 2.5 和 pretytable 模块,
我被卡住了请帮忙。谢谢
i am new in python so far i need help for this output from sqlite3
(u'GABRIELLA TEGGIA (40)', u'-', u'65')
(u'PUTRA', u'DPS', u'11')
(u'SUASTINI', u'-', u'-')
(u'ARI UTAMA', u'JL.D.BERATAN GG.8/4', u'26')
(u'SUYASA', u'DS.TUNJUK,TABANAN', u'33')
(u'SARAH(41)', u'BEJI AYU', u'40')
(u'AA.GDE OKA', u'JL.H.WURUK 179 DPS', u'53')
(u'AYU NILA RADIASTUTI', u'ABIAN BASE,KAPAL', u'8')
(u'DANTI I NYOMAN', u'GIANYAR', u'50')
(u'INDAYANI', u'BUNUTIN,UBUD', u'6')
(u'JENNI', u'JL.MUTIARA NO.18 KEROBOKAN', u'37')
(u'JERRY G.J ABELL', u'HOLLAND', u'-')
(u'COK ALIT', u'BR.MUKTI,SINGAPADU', u'35')
(u'SUPARTI NI PUTU', u'KARANGASEM', u'51')
(u'SUMADA I GD', u'SINGARAJA', u'60')
but i can only fetch only the last row like this one
+-------------+-----------+------+
| Pasien | Alamat | Umur |
+-------------+-----------+------+
| SUMADA I GD | SINGARAJA | 60 |
+-------------+-----------+------+
how to get the complete rows to print?
I provide with my code here:
import sqlite3
connection = sqlite3.connect('data.db3')
cursor=connection.cursor()
print("Show data row by row:")
print('-'*40)
from prettytable import PrettyTable
x = PrettyTable()
x.set_field_names(["Pasien", "Alamat", "Umur"])
cursor.execute('SELECT nm_pasien, almt_pasien, umur_pasien from pasien limit 15')
for nm_pasien in cursor:
print nm_pasien
print('-'*40)
x.add_row(nm_pasien)
x.printt()
I am using python 2.5 and pretytable module from here
i am stuck please help. thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
x.add_row
调用放在 for 循环内:在原始代码中,将
x.add_row(nm_pasien)
放在循环外部,add_row
只被调用一次。此外,nm_pasien
保留了 for 循环中的最后一个值。这就是为什么您只能看到 PrettyTable 中打印的最后一行数据。Put the
x.add_row
call inside the for-loop:In your orginial code, with
x.add_row(nm_pasien)
outside the loop,add_row
is only called once. Moreover,nm_pasien
retains its last value from the for-loop. This is why you only see the last row of data printed in the PrettyTable.