Page 67 - Python Cơ Bản
P. 67
OMВY Dictionary XLɳ Gɹ XLВ OLEM FɨS
import MySQLdb
db = MySQLdb.connect(host = 'localhost', user = 'ro
ot', passwd = 'root', db = 'mysql')
cursor = db.cursor(MySQLdb.cursors.DictCursor)
...
*IXGL Hо PMЖY
'ɹ QШX WР GɨGL чВ JIXGL Hо PMЖY XLɺRK HдRK Xк cursor WEY
OLM чɪ exectute(sql) Pɧ fetchone() Zɧ fetchall()
fetchone() GLИ JIXGL QШX HɸRK Hо PMЖY 2ЎY QYРR
JIXGL RLMАY HɸRK Hо PMЖY XLɳ Gɹ XLВ KМM RLMАY P϶R QЦM
P϶R WЌ XVϲ ZА QШX HɸRK Hо PMЖY 8РX GLS XVЬRK LвT XVY]
ZϴR VϴX RLMАY Hо PMЖY QШX PɾG 2ЎY XVϲ ZА None XиG Pɧ
чɪ JIXGL LЎX Hо PMЖY Xк GɩY XVY] ZϴR
fetchall() JIXGL XSɧR FШ Hо PMЖY XVY] ZϴR чвG Xк
GɩY XVY] ZϴR Zɧ XVϲ ZА QШX XYTPI GLиE GɨG HɸRK Hо