Nilesh Pant
Nilesh Pant

Reputation: 11

restart:shell as output when trying to connect mysql with python

import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456")
if mysql.connector.is_connected():
    print("connected")

I recently installed mysql.connector module (im a newb at python) and whilst trying to connect to the mysql database( i have everything installed and the database seems to be working perfectly fine on its own). This is the output that I get any time i try to run any code related to mysql.connector module.

this is the error : -->

========================================= RESTART: C:\\Users\\niles\\AppData\\Local\\Programs\\Python\\Python312\\hi.py =========================================

===================================================================== RESTART: Shell ====================================================================

i have searched the web for anything similar but found nothing i also tried is_connected variant to see if the connection's established, but it yielded the same result. any help appreciated

i looked for this on w3schools and yt videos and i expected to see something along the lines of this :

<mysql.connector.connection.MySQLConnection object ar 0x016645F0>

same -

========================================= RESTART: C:\\Users\\niles\\AppData\\Local\\Programs\\Python\\Python312\\hi.py =========================================

===================================================================== RESTART: Shell ====================================================================

output appears

Upvotes: -1

Views: 783

Answers (1)

Trevor
Trevor

Reputation: 11

I have been fighting with that recently as well for a project i am working on, I have finally succeeded in connecting, must be something to do with windows 11 anyway, instead of the mysql.connector.connect() method use the class mysql.connector.MySQLConnection() so

old code

mydb = mysql.connector.connect(host="localhost", user="root", password="123456")

new code

mydb = mysql.connector.MySQLConnection(host="localhost", user="root", password='123456', database='databasename')

hope that helps happy coding !!!

Upvotes: 1

Related Questions