Install MySql-Python Connector

Installation of MySql-Python Connector.

To establish Python and MySql connection to insert, update, delete and select, we need to install MySql and Python connector. Once the connector is installed, we can write a code in python using statement ‘import mysql.connector’.

First we install 64bit package MySql-Python connector in windows from site. This connector is an interface to connect to MySql database from python.

Verify in Python if connector is installed successfully.

To verify if the connector is installed properly, we use following statements.

In Python, Type the following statement. Specify host,user,password and database name Eg: as below.

>>import mysql.connector
>>mysql.connector.connect(host='localhost',database='test',user='root',password='root')

If installed it should display the following message

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

Another way is to write a code in python and test , if the connection is established to MySql database  or not.

Let us write a code in python with extension    .py Eg. Database_connection.py  as follows.

import mysql.connector
db=mysql.connector.connect(host='localhost',database='test',user='root',password='root')
if db:
 print "Connection Successful"
else:
 print "Connection Not Established"
exit

Run as c:>python database_connection.py. This should give output "Connection Successful".

The Python and MySQl database should have been previous installed and running.


Result



No comments:

Post a Comment