[Python] What is wrong with mysql.connector.connect?

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    DB: CREATE DATABASE IF NOT EXISTS test_schema CREATE TABLE city ( name varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; INSERT INTO city VALUES ('London'),('Paris'),('Oslo'); Python:

    import mysql.connector
    from mysql.connector import Error

    def f1()->None:
    connector = mysql.connector.connect(user='test_user',
    password='my_password_1',
    host='127.0.0.1',
    database='test_schema',
    port=3306,
    get_warnings = True)


    if (connector is None):
    print("Connection to DB is not set")
    return

    query = f"select name from city;"

    cursor = connector.cursor()
    result = None
    try:
    cursor.execute(query)
    result = cursor.fetchall()
    warnings = cursor.fetchwarnings()
    if warnings:
    print(f"Warnings: {warnings}")
    except Error as err:
    print(f"Error: {str(err)}")
    except Exception as ex:
    print(f"Query: \n{query} \n Exception:\n {str(ex)} \n")
    finally:
    cursor.close()
    connector.close()
    if result:
    print(f"Results: \n {result}")

    import pymysql
    import pymysql.cursors

    def f2()->None:
    connector = pymysql.connect(user='test_user',
    password='my_password_1',
    host='127.0.0.1',
    database='test_schema',
    cursorclass=pymysql.cursors.DictCursor)


    if (connector is None):
    print("Connection to DB is not set")
    return

    query = f"select name from city;"

    with connector.cursor() as cursor:
    cursor.execute(query, ())
    result = cursor.fetchall()
    if result:
    print(f"Results: \n {result}")
    else:
    print(f"Query returned nothing.")


    if __name__ == '__main__':
    f2()


    f1() doesn't work. No exceptions or warnings. Debug is went of on the first statement "mysql.connector.connect". Python 3.13.3 mysql-connector-python 9.3.0 OS Win 10.

    Continue reading...

Compartilhe esta Página