0

The program terminates automatically without throwin any error or output

The port and connector working fine and details are given below

C:\Users\jsr>pip list
Package                Version
---------------------- -------
mysql-connector-python 9.1.0
pip                    24.3.1

C:\Users\jsr>netstat -an | findstr 3306
  TCP    0.0.0.0:3306           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:33060          0.0.0.0:0              LISTENING
  TCP    [::]:3306              [::]:0                 LISTENING
  TCP    [::]:33060             [::]:0                 LISTENING

Python code-

import mysql.connector
from mysql.connector import Error

HOST = "localhost"
PORT = 3306
USER = "sud0077"
PASSWORD = "Admin@123"
DATABASE = "QuizzApp"

try:
    mydb = mysql.connector.connect(
        user=USER,
        password=PASSWORD,
        host=HOST,
        port=PORT,
        database=DATABASE
    )
    if mydb.is_connected():
        print("Connection successful")
except Error as e:
    print(f"Error: {e}")
finally:
    try:
        if mydb.is_connected():
            mydb.close()
            print("Connection closed")
    except NameError:
        print("Connection was never established")

Server is also running on port 3306. Id and pass is also correct but still no output terminal just gets closed while connecting and IDLE restarts automatically.enter image description here

6
  • why aint you using a framework like django for python and mysql? Django for example comes with ORM capabilities and migrations and you dont have to initialize db connectors like you are doing i guess Commented Jan 3 at 20:56
  • There's no way this can run without printing something. It should either print the sucess messages or the error messages. Commented Jan 3 at 20:57
  • 1
    There's little need to test if mydb.is_connected():. If it can't connect, it will raise an exception. Commented Jan 3 at 20:58
  • First: You try to connect to the database via CLI using the data that you put on your script? Like the host, user, password and dbname? Second: Remove the "finally" and try again Commented Jan 3 at 21:00
  • Do not use the admin password in any app. You would do well to set up access for that single app, with its own username, password, and table set. Also, check this answer: stackoverflow.com/questions/12600985/… Commented Jan 4 at 0:02

1 Answer 1

0

I tried to run the same code locally and it worked fine for me:

Screenshot of successful execution of code

Try to execute the script directly from PowerShell using the following command:

python mysql.py

To ensure that the problem is not the IDLE shell.

Sign up to request clarification or add additional context in comments.

3 Comments

I know its running on every single system, even my friends laptop too, but why not on my laptop .. i am really worried me and my friends tried every single possibilities, but in last, its not working :(
Did you tried to execute the script directly from PowerShell?
YES for more details I have shared i video link it will gives you clear picture that I am facing youtu.be/3vFn4vrDJEM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.