Setup connectivity between Python and MySQL

I want to built a connection between Python and MySQL via VS Code, for this where I have to write code for connectivity

pip install mysql-connector-python
You can try this to establish a connection between them.
Once installed, you can write Python code to establish the connection. Here’s a basic example:

import mysql.connector

connect= mysql.connector.connect(
host=“your_host”,
user=“your_username”,
password=“your_password”,
database=“your_database”
)

if connect.is_connected():
print(“Connected to MySQL”)
else:
print(“Connection failed”)
conn.close()
Thanks

Hi @jitenderjeeta2

To connect Python with MySQL using VS Code, you can follow these steps:

  1. Install Required Packages: Make sure you have the necessary Python packages installed. You’ll typically need mysql-connector-python, which you can install using pip:
pip install mysql-connector-python
  1. Import MySQL Connector: In your Python script, import the MySQL Connector module:
import mysql.connector
  1. Establish Connection: Use the connect() function from mysql.connector to establish a connection to your MySQL database. Provide the database credentials such as host, user, password, and database name:
# Database credentials
host = 'localhost'
user = 'your_username'
password = 'your_password'
database = 'your_database_name'

# Establish connection
connection = mysql.connector.connect(
    host=host,
    user=user,
    password=password,
    database=database
)
  1. Create Cursor Object: Once the connection is established, create a cursor object to execute SQL queries:
cursor = connection.cursor()
  1. Execute SQL Queries: Use the cursor object to execute SQL queries. For example, to fetch data from a table:
cursor.execute("SELECT * FROM your_table_name")

# Fetch the result
result = cursor.fetchall()

for row in result:
    print(row)
  1. Close Connection: After executing your queries, don’t forget to close the connection and cursor:
cursor.close()
connection.close()

Ensure that you replace 'your_username', 'your_password', 'your_database_name', and 'your_table_name' with your actual database credentials and table name.

You can write this code in a Python file within your VS Code project. Make sure you have MySQL running locally or provide the appropriate host information if your MySQL database is hosted elsewhere.

Do try this and let us know. If you still face any issue, please feel free to get back to us

I want to ask where I have to write this code for connectivity in my project face-recognition, whether in a separate file or within the same file

Hi @jitenderjeeta2

For simplicity and organization, it’s best to create a separate file named database.py within your face recognition project. Put the database connectivity code in this file. Then, import database.py into your main project file where you need to interact with the database. This approach keeps your code modular and organized.

Do try this and let us know.

Thank you so much for your help. I am now able to import database connectivity file in my project(Face-Recognition system)

Now I want that any face recognised by camera should be displayed into my database automatically with their name,id and some other attributes…
Have you any idea in this content, how it should be done? and How to write the code for it?

@jitenderjeeta2 We won’t be able to help with this as we have not worked particularly on this type projects. I would recommend that you look for resources related to the specific project that you are working on, on Google to find the best resources related to this project.