Import MySQLdb
db = MySQLdb.connect (host= ' localhost ', user= ' root ', passwd= ' 123456 ', db= ' database name ')
# Host: The database hostname. By default, the local host
# User: Database login name. The default is the current user
# passwd: The Secret of database landing. Default is Empty
# DB: The name of the database to use. No default value
db.select_db (' database name ')
# 2 Getting Cursors
cursor = Db.cursor ()
# 3 Manipulating Cursors
Try
# sql = "" "INSERT into student (ID, name, sex) VALUES (%s,%s,%s);" "
sql = "" "SELECT * from student;" "
Cursor.execute (SQL) # Execute SQL statement
# db.commit () # Modify the data in the table to be submitted
Print Cursor.fetchall ()
Except Exception as E:
Print E
Db.rollback ()
Finally
Cursor.close () # 4 Close cursor
Db.close () # 5 Close the database connection
In general, Python operation on MySQL 5 steps, first write, if there is insufficient please correct me
Python's basic operation for MySQL