Docy

connecting python With MYSQL

Estimated reading: 2 minutes 873 views

pymysql connection

in pymysql library we have methods from which we will connect the Python With mysql.

and this will be present to the libraray called as pymysql.

First we have to install this libraray.

pip install pymysql.

After this we have methods which will help us to connect.

connect()

using connect method we will give parameter as host,user,password and database name.

On successFul connection , it will retuen connection object

				
					conn = pymysql.connect(
        host='localhost',
        user='root',
        password="123456",
        db='sakila',
)
print(conn)
				
			

cursor()

Cursor() is the object used to interact with the database.

it allows python code to execyte MYSQL Command in a database session.

				
					cur = conn.cursor()
print(cur)
				
			

execute()

as the name suggest execute() is used to execute the sql String to sql query into the database.

				
					string_aquery = "SELECT VERSION()"

cur.execute(string_aquery)
				
			

fetchall()

The fetcall() gets all the records.
and its return Type as a tuple of tuples.
Each of the inner tuples repersents a row in the table

				
					output = cur.fetchall()
print(output)
				
			

Leave a Comment

Share this Doc
CONTENTS