如何从 pymysql.cursor.description 中获取所有数据类型的映射?
How to get a map of all data types from pymysql.cursor.description?
对于sql_select_query = 'select * from table',可以使用pymysql
python模块连接到MySql数据库,cursor.description提取值.
def conn():
myDb=pymysql.connect(server,user,password,database)
return myDb
dbc = conn() #database connection
dbCursor = dbc.cursor() # cursor
# gathers all column field names and their type
field_names_and_type = [desc[:2] for desc in dbCursor.description]
示例输出:
print(field_names_and_type)
[('ItemId', 1), ('ItemName', 3)]
类型 1 是 nvchar
类型 3 是 int
问题:我如何映射这些?
我检查了 pymysql docs 但找不到 cursor.description 输出的映射。
Pymysql
类型代码在 pymysql.constants.FIELD_TYPE
中定义
DECIMAL = 0
TINY = 1
SHORT = 2
LONG = 3
FLOAT = 4
DOUBLE = 5
NULL = 6
TIMESTAMP = 7
LONGLONG = 8
INT24 = 9
DATE = 10
TIME = 11
DATETIME = 12
YEAR = 13
NEWDATE = 14
VARCHAR = 15
BIT = 16
JSON = 245
NEWDECIMAL = 246
ENUM = 247
SET = 248
TINY_BLOB = 249
MEDIUM_BLOB = 250
LONG_BLOB = 251
BLOB = 252
VAR_STRING = 253
STRING = 254
GEOMETRY = 255
CHAR = TINY
INTERVAL = ENUM
对于sql_select_query = 'select * from table',可以使用pymysql
python模块连接到MySql数据库,cursor.description提取值.
def conn():
myDb=pymysql.connect(server,user,password,database)
return myDb
dbc = conn() #database connection
dbCursor = dbc.cursor() # cursor
# gathers all column field names and their type
field_names_and_type = [desc[:2] for desc in dbCursor.description]
示例输出:
print(field_names_and_type)
[('ItemId', 1), ('ItemName', 3)]
类型 1 是 nvchar
类型 3 是 int
问题:我如何映射这些? 我检查了 pymysql docs 但找不到 cursor.description 输出的映射。
Pymysql
类型代码在 pymysql.constants.FIELD_TYPE
DECIMAL = 0
TINY = 1
SHORT = 2
LONG = 3
FLOAT = 4
DOUBLE = 5
NULL = 6
TIMESTAMP = 7
LONGLONG = 8
INT24 = 9
DATE = 10
TIME = 11
DATETIME = 12
YEAR = 13
NEWDATE = 14
VARCHAR = 15
BIT = 16
JSON = 245
NEWDECIMAL = 246
ENUM = 247
SET = 248
TINY_BLOB = 249
MEDIUM_BLOB = 250
LONG_BLOB = 251
BLOB = 252
VAR_STRING = 253
STRING = 254
GEOMETRY = 255
CHAR = TINY
INTERVAL = ENUM