Use MySQL from Python

MySQL seems to be coming out soon, but I will use it normally, so make a note of how to use it from Python.

Library installation

Use MySQL-python. By the way, this library seems to be compatible with MariaDB.

python


pip install MySQL-python

If an error occurs in debian system apt-get install python-dev May be resolved by running

How to use

python


import MySQLdb
#Login to DB
#Optional for localhost
connection = MySQLdb.connect(db="test",user="test")

cursor = connection.cursor()
# SQL
cursor.execute("select * from users")
result = cursor.fetchall()

for row in result:
 p row[0]

cursor.close()
connection.close()

Then, you can get the execution result of the SQL statement as an array row by row as a row. Also, if you specify cursorclass = MySQLdb.cursors.DictCursor for MySQLdb.connect, row becomes a dictionary of column names and values.

Recommended Posts

Use MySQL from Python
Use MySQL from Python
Use MySQL from Anaconda (python)
Use thingsspeak from python
Touch MySQL from Python 3
Use fluentd from python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
Use e-Stat API from Python
Use Stanford Core NLP from Python
Read and use Python files from Python
Forcibly use Google Translate from python
Use kabu Station® API from Python
How to use Mysql in python
Use Azure Blob Storage from Python
Use the Flickr API from Python
Use fastText trained model from Python
Use Google Analytics API from Python
sql from python
Receive textual data from mysql with python
I want to use jar from python
Connecting from python to MySQL on CentOS 6.4
Use Django from a local Python script
Use C ++ functions from python with pybind11
Use config.ini in Python
Operate Filemaker from Python
[Python] Use JSON with Python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Study from Python Hour7: How to use classes
Python from or import
Use Valgrind in Python
Use mecab with Python3
Use LiquidTap Python Client ③
Run python from excel
Install python from source
Wrap C with Cython for use from Python
Write Python in MySQL
Operate neutron from Python!
Use Python 3.8 with Anaconda
Use Python in your environment from Win Automation
Use python with docker
I want to use ceres solver from python
Operate LXC from Python
Use LiquidTap Python Client ②
Manipulate riak from python
Force Python from Fortran
Use profiler in Python
Connect python to mysql
Use MySQL with Django
Let's use different versions of SQLite3 from Python3!
Execute command from python
Wrap C ++ with Cython for use from Python
Use the nghttp2 Python module from Homebrew from pyenv's Python
[Python] Read From Stdin
Use Tor to connect from urllib2 [Python] [Mac]
Python: Use zipfile to unzip from standard input
Use LiquidTap Python Client ①
Use R density ratio estimation package densratio from Python