Use MySQL from Python

Reference: Using MySQL from Python

# coding: utf-8

import MySQLdb


def main():
    conn = MySQLdb.connect(
        user='testuser',
        passwd='testuser',
        host='192.168.33.3',
        db='testdb'
    )
    c = conn.cursor()

#Create table sql = 'create table test (id int, content varchar(32))' c.execute(sql) print ('* create test table \ n')

#Get table list sql = 'show tables' c.execute(sql) print ('===== table list =====') print(c.fetchone())

#Record registration sql = 'insert into test values (%s, %s)' c.execute (sql, (1,'hoge')) # 1 only datas = [ (2, 'foo'), (3, 'bar') ] c.executemany (sql, datas) # Multiple print ('\ n * Register 3 records \ n')

#Get record sql = 'select * from test' c.execute(sql) print ('===== record =====') for row in c.fetchall(): print('Id:', row[0], 'Content:', row[1])

#Delete record sql = 'delete from test where id=%s' c.execute(sql, (2,)) print ('\ n * delete record with id 2 \ n')

#Get record sql = 'select * from test' c.execute(sql) print ('===== record =====') for row in c.fetchall(): print('Id:', row[0], 'Content:', row[1])

#Save changes to database conn.commit()

    c.close()
    conn.close()


if __name__ == '__main__':
    main()

Execution result

===== Table list ===== ('test',)

===== Record ===== Id: 1 Content: hoge Id: 2 Content: foo Id: 3 Content: bar

===== Record ===== Id: 1 Content: hoge Id: 3 Content: bar

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
MeCab from Python
Receive textual data from mysql with python
Use PostgreSQL data type (jsonb) from Python
Use machine learning APIs A3RT from Python
I want to use jar from python
Connecting from python to MySQL on CentOS 6.4
Use Google Cloud Vision API from Python
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
Use dates in 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
Use DynamoDB with Python
[Bash] Use here-documents to get python power from bash
Wrap C with Cython for use from Python
Execute command from Python
Write Python in MySQL
Operate neutron from Python!
Use Python 3.8 with Anaconda
Use Python in your environment from Win Automation
[Python] format methodical use
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