In this article, I will share with you how to connect the MySQL database in Python using mysql.connector. as you know if you want to make web applications in python the first and very important step is to use any database to store all the records. so, here in this article, I show you how to use the MySQL database in Python.
Python provides mysql.connector
library for connection with MySQL. so, here we also use this library and make connections between MySQL and Python.
After, the connection successfully then you can store, fetch, and update any record in the database help of Python code.
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='myfirstpythonapp',
user='root',
password='root')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
i hope you like this article.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to enable SSH access in Cpanel and connect to remote server
When working on Cpanel server, sometimes...9 basic commands that every new Linux users need to know
Whenever thinking about Linux, most of p...Show read more link if the text exceeds a certain length using jQuery
Use the JavaScript substring() ...Laravel 8 Create CRUD in Livewire with Bootstrap Model Validation
In this blog, I would relish to apportio...Ajax CRUD example in Laravel 5.5 application
Hello, everyone in this post we share wi...