Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Failure: test-side #288

Open
hengyoush opened this issue Feb 7, 2025 · 1 comment · Fixed by #293
Open

Test Failure: test-side #288

hengyoush opened this issue Feb 7, 2025 · 1 comment · Fixed by #293
Labels
bug Something isn't working

Comments

@hengyoush
Copy link
Owner

Describe the bug
logs: https://github.com/hengyoush/kyanos/actions/runs/13170107381/job/36826916113

Traceback (most recent call last):
  File "/host/./testdata/query_mysql.py", line 58, in <module>
    create_database(connection, database_name)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/host/./testdata/query_mysql.py", line 21, in create_database
    cursor = connection.cursor()
             ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'cursor'

To Reproduce
Steps to reproduce the behavior:

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Information (please complete the following information):

  • OS: [e.g. use uname -ar]
Copy link

dosubot bot commented Feb 7, 2025

To resolve the AttributeError in the query_mysql.py script, ensure that the connection to the MySQL server is successfully established before using the cursor() method. The error occurs because the create_connection function might return None if the connection fails. Here's how you can modify the script to handle this situation:

import mysql.connector  
import time
import sys
from mysql.connector import Error  
  
def create_connection(host_name, user_name, user_password):  
    connection = None  
    try:  
        connection = mysql.connector.connect(  
            host=host_name,  
            user=user_name,  
            passwd=user_password  
        )  
        print("Connection to MySQL server successful")  
    except Error as e:  
        print(f"The error '{e}' occurred")  
      
    return connection  
  
def create_database(connection, database_name):  
    if connection is None:
        print("No connection available to create the database.")
        return
    cursor = connection.cursor()  
    try:  
        cursor.execute(f"CREATE DATABASE {database_name}")  
        print(f"Database '{database_name}' created successfully")  
    except Error as e:  
        print(f"The error '{e}' occurred")  
  
def execute_query_in_database(connection, database_name, query, count):  
    if connection is None:
        print("No connection available to execute the query.")
        return
    try:  
        cursor = connection.cursor()  
        cursor.execute(f"USE {database_name}")  
        for i in range(count):
            cursor.execute(query)  
            
            if query.strip().upper().startswith('SELECT'):  
                result = cursor.fetchall()  
                for row in result:  
                    print(row)  
            else:  
                connection.commit()  
                print("Query executed successfully")  
            time.sleep(0.5)
          
    except Error as e:  
        print(f"The error '{e}' occurred")  
  
count = int(sys.argv[1])
connection = create_connection("localhost", "root", "123456")  

if connection is None:
    print("Failed to create a connection to the database. Exiting the script.")
    sys.exit(1)

database_name = "test"  
create_database(connection, database_name)  

query = "CREATE TABLE example_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100))"  
execute_query_in_database(connection, database_name, query, 1)  

insert_query = "INSERT INTO example_table (name) VALUES ('John Doe')"  
execute_query_in_database(connection, database_name, insert_query, 1)  

select_query = "SELECT * FROM example_table"  
execute_query_in_database(connection, database_name, select_query, count) 

time.sleep(1)
if connection.is_connected():  
    connection.close()  
    print("The connection is closed")

This modification checks if the connection is None after attempting to create it. If it is None, the script prints an error message and exits, preventing any further operations that would cause an AttributeError [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@dosubot dosubot bot added the bug Something isn't working label Feb 7, 2025
@hengyoush hengyoush reopened this Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant