Download MySQL in Python - BIOTEC TU Dresden

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ProgramminginPython
Lecture7:MySQLinPython
MichaelSchroeder
Sebas0anSalen0n
1
SlidesderivedfromIanHolmes,DepartmentofSta0s0cs,UniversityofOxford
TheMySQLPythonAPI
• Communica0ontoMySQLviaAPI
• Applica0onProgrammingInterface
• clearlydefinedsetofrou0nes
• Canbemappedtootherlanguages
• Inthiscase,MySQLtoPython
• QueriescanbesentinSQL
• UseofvariablesanddataloadedintoPython
• Directprocessingofresults
• Enablesautoma0onofqueries,DBcrea0on,...
2
Connec0ngtoadatabase
Python module for the MySQL API
import MySQLdb
# Connection to database with access specification
conn = MySQLdb.connect(db=”pet",
# name of database
host=”mydb",
# name of server
user="guest",
# username
passwd="guest") # password
# Let’s see what’s stored in conn
print conn
# close connection
conn.close()
<_mysql.connection open to ‘mydb’ at 235b980>
Toperformac2onsonthedatabase,wewillusefunc2onsac2ngontheopenconnec2on!
3
Usingcursors
Thedifferentcursorclassesinstan2ateobjectstoexecuteSQLstatements.
This special cursor returns rows as dictionaries
# ‘conn’ is our connection instance
from MySQLdb.cursors import DictCursor
cursor = conn.cursor(DictCursor) # Create a new cursor instance
print cursor # Let’s see what’s stored in the cursor
# send a query
cursor.execute("SELECT name,owner FROM pet LIMIT 4")
# retrieve all rows as a list of dictionaries
data = cursor.fetchone()
fetchall() for
all rows
print data
# close connection
conn.close()
<MySQLdb.cursors.DictCursor object at 0x7fd185187090>
{‘owner’: ‘Gwen’, ‘name’: ‘Whistler’}
4
Syntax
Crea0ngtables
CREATE TABLE table_name (create definition)
Example
CREATE TABLE user_guest.sebastian_work (email VARCHAR(100) NOT NULL,
website VARCHAR(200),
employee_id INT NOT NULL)
inPython
# ‘cursor’ is our DictCursor instance
sqlquery = “CREATE TABLE user_guest.sebastian_work (
email VARCHAR(100) NOT NULL,
website VARCHAR(200),
employee_id INT NOT NULL)”
# execute query
cursor.execute(sqlquery)
5
Syntax
Dele0ngtables
DROP TABLE table_name
Example
DROP TABLE user_guest.sebastian_work
inPython
# ‘cursor’ is our DictCursor instance
sqlquery = “DROP TABLE user_guest.sebastian_work”
# execute query
cursor.execute(sqlquery)
6
Syntax
Inser0ngdatafromvariables
INSERT INTO table_name VALUES (values)
Example
INSERT INTO user_guest.sebastian_work VALUES (‘[email protected]’,
‘biotec.xyz/~sebastian’,
142)
inPython
# ‘cursor’ is our DictCursor instance
values = (‘[email protected]’, ‘biotec.xyz/~sebastian’, 142)
sqlquery = “INSERT INTO user_guest.sebastian_work VALUES (%s, %s, %s)”
# execute query
cursor.execute(sqlquery, values)
7
DealingwithNULL
NULLinMySQLismappedtoNoneinPython
Example
INSERT INTO user_guest.sebastian_work VALUES (‘[email protected]’,
None,
143)
inPython
# ‘cursor’ is our DictCursor instance
values = (‘[email protected]’, None, 143)
sqlquery = “INSERT INTO user_guest.sebastian_work VALUES (%s, %s, %s)”
# execute query
cursor.execute(sqlquery, values)
email
website
employee_id
[email protected]
NULL
143
8