Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python mysql连接器隐式地将字符串转换为整数_Python_Mysql_Mysql Connector Python - Fatal编程技术网

Python mysql连接器隐式地将字符串转换为整数

Python mysql连接器隐式地将字符串转换为整数,python,mysql,mysql-connector-python,Python,Mysql,Mysql Connector Python,我有一个mysql表,具有以下模式和表 CREATE SCHEMA IF NOT EXISTS test DEFAULT CHARACTER SET utf8; USE test; CREATE TABLE IF NOT EXISTS test.test_table ( _id_pn INTEGER NOT NULL AUTO_INCREMENT, _element TEXT CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci

我有一个mysql表,具有以下模式和表

CREATE SCHEMA IF NOT EXISTS test DEFAULT CHARACTER SET utf8;
USE test;

CREATE TABLE IF NOT EXISTS test.test_table (
    _id_pn INTEGER NOT NULL AUTO_INCREMENT,
    _element TEXT CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci' NOT NULL,
    PRIMARY KEY (_id_pn)) ENGINE = InnoDB;
我使用mysql-python连接器连接到数据库并执行如下查询

import mysql.connector

MYSQL_CONFIG = {
    'autocommit': True,
    'ssl_disabled': True,
    'host': '127.0.0.1',
    'port': 3307,
    'user': 'root',
    'password': 'toor',
    'pool_size': 1,
    'database': 'test'
}

def insert_into_table(cursor, element):
    cursor.execute("INSERT INTO test_table (_element) values (%s)", (element,))

def print_table_values(cursor):
    cursor.execute("SELECT _element from test_table")
    for row in cursor:
        print(row['_element'])
        print(type(row['_element']))

def print_table_values_raw_cursor(cursor):
    cursor.execute("SELECT _element from test_table")
    for (elem,) in cursor:
        decoded_elem = elem.decode('utf-8')
        print(decoded_elem)
        print(type(decoded_elem))

cnx = mysql.connector.connect(**MYSQL_CONFIG)
cursor = cnx.cursor(buffered=True, dictionary=True)
insert_into_table(cursor, "000001")
print_table_values(cursor)

cursor2 = cnx.cursor(buffered=True, raw=True)
print_table_values_raw_cursor(cursor2)

cursor.close()
cursor2.close()
cnx.close()
运行此代码的结果是

1
<type 'int'>
000001
<type 'unicode'>
1
000001
有人能解释一下为什么代码会这样做吗? 另外,使用原始游标和手动解码值是一个好主意吗

Python版本=2.7.10

Python mysql连接器版本=8.0.16

Mysql服务器版本=5.6.44-log Mysql社区服务器(GPL)