简体   繁体   中英

How to retrieve zero fill column from mysql via python?

I have a table in mysql as below:

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

note that first column (pid) is zero fill column and the data in the table(province) is as below:

=========================================
|pid|pname|family|population|male|female|
=========================================
|02 | 'A' |  12  |    20    |  8 |   5  |
=========================================
|03 | 'B' |  25  |    20    |  7 |   6  |
=========================================
|05 | 'c' |  34  |     5    |  7 |   9  |
=========================================

I want to retrieve pid column via python so my python code is:

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()     

but when i run this python code this Exception occurred:

returned a result with an error set File "C:\\Users\\M_Parastar\\Desktop\\New folder\\ttt.py", line 11, in print(cursor.fetchall())


Do you have any idea how to retrieve zero fill column via python??

The cursor returns an iterable python object, replace "print(cursor.fetchall())" with "for rows in cursor: print (rows)". Visit Connector/Python API Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM