我在mysql中有一个表,如下所示:
CREATE TABLE `province` (
`pid` int(2) unsigned zerofill NOT NULL,
`pname` varchar(255) CHARACTER SET utf8 COLLATE utf8_persian_ci DEFAULT NULL,
`family` int(12) DEFAULT NULL,
`population` int(11) DEFAULT NULL,
`male` int(11) DEFAULT NULL,
`female` int(11) DEFAULT NULL,
PRIMARY KEY (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci
请注意,第一列(pid)为零填充列 表(省)中的数据如下:
=========================================
|pid|pname|family|population|male|female|
=========================================
|02 | 'A' | 12 | 20 | 8 | 5 |
=========================================
|03 | 'B' | 25 | 20 | 7 | 6 |
=========================================
|05 | 'c' | 34 | 5 | 7 | 9 |
=========================================
我想通过python检索pid列,所以我的python代码是:
import mysql.connector
if __name__ == '__main__':
data = []
res = []
cnx = mysql.connector.connect(user='mehdi', password='mehdi', host='127.0.0.1', database='cra_db')
cursor = cnx.cursor()
qrystr = 'SELECT pid FROM province;'
cursor.execute(qrystr)
print(cursor.fetchall())
cnx.close()
但是当我运行此python代码时,发生此异常:
返回的结果带有错误集 第11行中的文件“ C:\ Users \ M_Parastar \ Desktop \ New folder \ ttt.py” 打印(cursor.fetchall())
您是否知道如何通过python检索零填充列?
答案 0 :(得分:0)
游标返回一个可迭代的python对象,将“ print(cursor.fetchall())”替换为“对于游标中的行:打印(行)”。访问Connector/Python API Reference