INSERT into MySQL with Python [For beginners]

◎ Refer to the following and try the connect to DB and SELECT statement. Package uses mysqlclient How to connect to MySQL with Python [For beginners]

If you write to connect to MySQL with Python, an error will occur as it is, so use the following.

#Use the mysqlclient package
import MySQLdb

#Connecting
conn = MySQLdb.connect(
user='root',
passwd='root',
host='localhost',
db='mstibqym_crontest')

#Get the cursor
cur = conn.cursor()

#Execute SQL (command to operate the database)
sql = "select * from test_table"
cur.execute(sql)

#Get the execution result
rows = cur.fetchall()

#Display line by line
for row in rows:
    print(row)

cur.close

#Close connection
conn.close

◎ For INSERT, refer to the following Using MySQL with Python3 – with sample code from basic operation to error handling

#Use the mysqlclient package
import MySQLdb

#Connecting
conn = MySQLdb.connect(
user='root',
passwd='root',
host='localhost',
db='mstibqym_crontest')

#Get the cursor
cur = conn.cursor()

#Execute SQL (command to operate the database)
sql = "INSERT INTO test_table (item) VALUES('xyzss')"
cur.execute(sql)

conn.commit()
cur.close

#Close connection
conn.close

Recommended Posts

INSERT into MySQL with Python [For beginners]
[Introduction for beginners] Working with MySQL in Python
Debug for mysql connection with python mysql.connector
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
[For beginners] Try web scraping with Python
python textbook for beginners
OpenCV for Python beginners
Causal reasoning and causal search with Python (for beginners)
~ Tips for Python beginners from Pythonista with love ① ~
~ Tips for Python beginners from Pythonista with love ② ~
Learning flow for Python beginners
Python3 environment construction (for beginners)
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Connection pooling with Python + MySQL
[Python] GUI for inserting TeX format into PowerPoint with 2 clicks [PowerPoint]
[For beginners] Summary of standard input in Python (with explanation)
Stylish technique for pasting CSV data into Excel with Python
Solve AtCoder Problems Boot camp for Beginners (Medium 100) with python
Python Exercise for Beginners # 2 [for Statement / While Statement]
Python for super beginners Python # dictionary type 1 for super beginners
Build Mysql + Python environment with docker
Getting Started with Python for PHPer-Classes
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
Python #len function for super beginners
Beginners use Python for web scraping (1)
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
Getting Started with Python for PHPer-Functions
[Pandas] I tried to analyze sales data with Python [For beginners]
[For beginners] Read Excel / CSV files into DataFrame with Google Colaboratory
Easy keyword extraction with TermExtract for Python
Receive textual data from mysql with python
WEB scraping with Python (for personal notes)
[Python] Minutes of study meeting for beginners (7/15)
Manually ssh registration for coreserver with python
Use DeepL with python (for dissertation translation)
Memo to ask for KPI with python
Amplify images for machine learning with python
[Episode 2] Beginners tried Numeron AI with python
[Episode 3] Beginners tried Numeron AI with python
Tips for dealing with binaries in Python
Put protocol buffers into sqlite with python
Connect to MySQL with Python within Docker
Let's put together Python for super beginners
Tips for using python + caffe with TSUBAME
[Shakyo] Encounter with Python for machine learning
Process multiple lists with for in Python
[Episode 0] Beginners tried Numeron AI with python
Getting Started with Python for PHPer-Super Basics
[Episode 1] Beginners tried Numeron AI with python
Let's do MySQL data manipulation with Python
How Python beginners get started with Python with Progete