如何通过python从mysql中检索zero-fill列?

2024-06-01 20:18:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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代码时,发生了一个异常:

返回了一个带有错误集的结果 文件“C:\Users\M\u Parastar\Desktop\New folder\ttt.py公司,第11行,在 打印(游标.fetchall())


你知道如何通过python检索zero fill列吗??你知道吗


Tags: cidefaultmysqlutf8familypidnullcursor