◎ 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