I have a question to ask, for MySQL database, there are column names such as lecturer_name, lecturer_id and more. So what I wanted to ask is that what are the way(s) to code the data into Python so that it retrieves the values for processing?
Asked
Active
Viewed 93 times
-1
-
This is a very broad question and a very common use case. A google search would turn up countless tutorials on how to access MySQL db with python. – SuperShoot Jul 21 '17 at 04:36
-
[possible duplicate](https://stackoverflow.com/questions/372885/how-do-i-connect-to-a-mysql-database-in-python) – SuperShoot Jul 21 '17 at 04:37
1 Answers
0
Basic access: install a dbapi driver for python.
theres the official mysql-connector-python
sudo pip install mysql-connector
basic usage looks something like this:
import mysql.connector
# connect
connect = mysql.connector.connect(user='scott', database='employees', password='', host='some ip')
# get a cursor/buffer
cursor = connect.cursor()
# execute query
cursor.execute("select * from mycooltable")
# get data back row by row.
for (colA, colB, colC , ... ) in cursor:
... do some work ...
the driver will map the MySQL types to python types, though complex types.
Depending on need, you may want to use some kind of framework that makes manipulating the data easier (ORM).
python-etl is a good minimalistic framework,
pip install petl
or if need be go to a bigger framework like SQLAlchemy, depending on need.
See this for similar. How do I connect to a MySQL Database in Python?

Xingzhou Liu
- 1,507
- 8
- 12