how fetchall sqlite3 data into pretytable printing in python
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.curso开发者_C百科r()
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
Put the x.add_row
call inside the for-loop:
for nm_pasien in cursor:
x.add_row(nm_pasien)
# print nm_pasien
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.
精彩评论