Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Getting Started with MySQL and Python https://stackabuse.

com/getting-started-with-mysql-and-python/

$ pip install mysql-connector-python

1 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

$ pip install mysql-connector-python==<insert_version_number_here>

$ pip install mysqlclient

import MySQLdb

pythondb

CREATE DATABASE pythondb;


USE pythondb;

pythonuser pythonpwd123
pythondb

CREATE USER 'pythonuser'@'localhost' IDENTIFIED BY 'pythonpwd123'


GRANT ALL PRIVILEGES ON pythondb.* To 'pythonuser'@'localhost'
FLUSH PRIVILEGES

2 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

#!/usr/bin/python

import MySQLdb

dbconnect = MySQLdb.connect("localhost", "pythonuser", "pythonpwd123", "pythondb")

cursor = dbconnect.cursor()
cursor.execute("SELECT VERSION()")

data = cursor.fetchone()
if data:
print('Version retrieved: ', data)
else:
print('Version not retrieved.')

dbconnect.close()

Version retrieved: 5.7.19

import MySQLdb

MySQLdb.connect()

dbconnect

MySQLCursor
execute
fetchall fetchone fetchmany

3 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

dbconnect.commit()

dbconnect.rollback()

dbconnect.close()

pythondb

4 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

import MySQLdb

dbconnect = MySQLdb.connect("localhost","pythonuser","pythonpwd123","pythondb" )

cursor = dbconnect.cursor()
cursor.execute("DROP TABLE IF EXISTS MOVIE")

query = "CREATE TABLE MOVIE( \


id int(11) NOT NULL,\
name varchar(20),\
year int(11),\
director varchar(20),\
genre varchar(20),\
PRIMARY KEY (id))"

cursor.execute(query)

dbconnect.close()

movie
pythondb

5 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

#!/usr/bin/python

import MySQLdb

dbconnect = MySQLdb.connect("localhost", "pythonuser", "pythonpwd123", "pythondb")

cursor = dbconnect.cursor()

query = 'insert into movie(id, name, year, director, genre) \


values (1, "Bruce Almighty", 2003, "Tom Shaydac", "Comedy")'
try:
cursor.execute(query)
dbconnect.commit()
except:
dbconnect.rollback()
finally:
dbconnect.close()

cursor.fetchall()
cursor.fetchmany()
cursor.fetchone()

fetchall

6 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

#!/usr/bin/python

import MySQLdb

dbconnect = MySQLdb.connect("localhost", "pythonuser", "pythonpwd123", "pythondb")

cursor = dbconnect.cursor()

query = "SELECT * FROM movie"


try:
cursor.execute(query)
resultList = cursor.fetchall()
for row in resultList:
print ("Movie ID =", row[0])
print ("Name =", row[1])
print ("Year =", row[2])
print ("Director = ", row[3])
print ('Genre = ', row[4])
except:
print ("Encountered error while retrieving data from database")
finally:
dbconnect.close()

Movie ID = 1
Name = Bruce Almighty
Year = 2003
Director = Tom Shaydac
Genre = Comedy

import MySQLdb

dbconnect = MySQLdb.connect("localhost", "pythonuser", "pythonpwd123", "pythondb")

# The cursor object obtained below allows SQL queries to be executed in the database session.
cursor = dbconnect.cursor()

updatequery = "update movie set genre = 'Satire' where id = 1"

cursor.execute(updatequery)

dbconnect.commit()

print(cursor.rowcount, "record(s) affected")

7 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

1 record(s) affected

import MySQLdb

dbconnect = MySQLdb.connect("localhost", "pythonuser", "pythonpwd123", "pythondb")

# The cursor object obtained below allows SQL queries to be executed in the database session.
cursor = dbconnect.cursor()

updatequery = "DELETE FROM movie WHERE id = 1"

cursor.execute(updatequery)

dbconnect.commit()

print(cursor.rowcount, "record(s) deleted")

1 record(s) deleted

8 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

9 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

10 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

11 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/

12 of 12 11/06/2020, 14:12

You might also like