Getting Started With MySQL and Python PDF
Getting Started With MySQL and Python PDF
com/getting-started-with-mysql-and-python/
1 of 12 11/06/2020, 14:12
Getting Started with MySQL and Python https://stackabuse.com/getting-started-with-mysql-and-python/
import MySQLdb
pythondb
pythonuser pythonpwd123
pythondb
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
cursor = dbconnect.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
if data:
print('Version retrieved: ', data)
else:
print('Version not retrieved.')
dbconnect.close()
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")
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
cursor = dbconnect.cursor()
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
cursor = dbconnect.cursor()
Movie ID = 1
Name = Bruce Almighty
Year = 2003
Director = Tom Shaydac
Genre = Comedy
import MySQLdb
# The cursor object obtained below allows SQL queries to be executed in the database session.
cursor = dbconnect.cursor()
cursor.execute(updatequery)
dbconnect.commit()
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
# The cursor object obtained below allows SQL queries to be executed in the database session.
cursor = dbconnect.cursor()
cursor.execute(updatequery)
dbconnect.commit()
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