Python 到 mySql 工作台((类型错误:%d 格式:需要数字,而不是 str))

问题描述 投票:0回答:1

我正在为我的班级做一些作业,并且我已成功将我的 python 连接到 mysql 工作台 显示连接正常 截图显示了python和sql的连接,那么我的代码有错吗? 最终,我的人工智能需要协助处理天气状况

import mysql.connector
from mysql.connector import Error

# Define the connection details
config = {
    'user': 'chatbot',  
    'password': 'PASSWORD1', 
    'host': 'hostname',  
    'database': 'MC_chatbot',  
    'port': 3306 
}

try:
    # Connect to the database
    connection = mysql.connector.connect(**config)
    if connection.is_connected():
        print("Connected to the database successfully")
        cursor = connection.cursor()

        # Function to fetch the weather response
        def get_weather_response():
            query = "SELECT response1, response2, response3, response4, response5, response6, response7, response8 FROM chat WHERE keyword = 'weather'"
            cursor.execute(query)
            result = cursor.fetchone()  # Fetch the first row of the result
            if result:
                return [response for response in result if response]  # Return non-null responses
            else:
                return None

        # Function to handle user input and provide response
        def handle_user_input(user_input):
            if 'weather' in user_input.lower():
                responses = get_weather_response()
                if responses:
                    return responses[0]  # For simplicity, return the first response
                else:
                    return "Sorry, I don't have any information on the weather."
            else:
                return "I can only help with weather-related questions."

        # Example interaction
        user_input = input("You: ")
        response = handle_user_input(user_input)
        print("Bot:", response)

except Error as e:
    print(f"Error: {e}")

finally:
    # Close the connection
    if 'connection' in locals() and connection.is_connected():
        cursor.close()
        connection.close()
        print("Connection closed")

当我运行 python 脚本时,我收到以下内容:

Traceback (most recent call last):
  File "C:\Users\HOME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\network.py", line 728, in open_connection
    addrinfos = socket.getaddrinfo(
                ^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\HOME\Desktop\Capella\AI v2\Week 6\Assignment 6.py", line 13, in <module>
    connection = mysql.connector.connect(**config)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HOME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\pooling.py", line 323, in connect
    return MySQLConnection(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HOME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\connection.py", line 180, in __init__
    self.connect(**kwargs)
  File "C:\Users\HOME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\abstracts.py", line 1360, in connect
    self._open_connection()
  File "C:\Users\HOME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\connection.py", line 359, in _open_connection
    self._socket.open_connection()
  File "C:\Users\HOME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\network.py", line 748, in open_connection
    raise InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '%-.100s:%u' (%s) (Warning: %u format: a real number is required, not str)
python mysql-connector
1个回答
0
投票

您必须在配置中将主机替换为 localhost,而不是使用主机名。

© www.soinside.com 2019 - 2024. All rights reserved.