TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python MySQL Database Connection

Python MySQL Database Connection with python tutorial, overview, environment set-up, first python program, basics, data types, operators, if-else statements, loops, history, features, history, versions, example, operators, variables etc.

<< Back to PYTHON

Database Connection

In this section of the tutorial, we will discuss the steps to connect the python application to the database.

There are the following steps to connect a python application to our database.

  1. Import mysql.connector module
  2. Create the connection object.
  3. Create the cursor object
  4. Execute the query

Creating the connection

To create a connection between the MySQL database and the python application, the connect() method of mysql.connector module is used.

Pass the database details like HostName, username, and the database password in the method call. The method returns the connection object.

The syntax to use the connect() is given below.

Connection-Object= mysql.connector.connect(host = <host-name> , user = <username> , passwd = <password> )

Consider the following example.

Example

import mysql.connector

#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google")

#printing the connection object 
print(myconn)

Output:

<mysql.connector.connection.MySQLConnection object at 0x7fb142edd780> 

Here, we must notice that we can specify the database name in the connect() method if we want to connect to a specific database.

Example

import mysql.connector

#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google", database = "mydb")

#printing the connection object 
print(myconn) 

Output:

<mysql.connector.connection.MySQLConnection object at 0x7ff64aa3d7b8> 

Creating a cursor object

The cursor object can be defined as an abstraction specified in the Python DB-API 2.0. It facilitates us to have multiple separate working environments through the same connection to the database. We can create the cursor object by calling the 'cursor' function of the connection object. The cursor object is an important aspect of executing queries to the databases.

The syntax to create the cursor object is given below.

<my_cur>  = conn.cursor()

Example

import mysql.connector
#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google", database = "mydb")

#printing the connection object 
print(myconn) 

#creating the cursor object
cur = myconn.cursor()

print(cur)

Output:

<mysql.connector.connection.MySQLConnection object at 0x7faa17a15748> 
MySQLCursor: (Nothing executed yet)




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf