Python's interaction with MySQL
1. Installing the MySQL module
PIP3 Install Pymysql
2. Connection object
Used to establish a connection to the database
2.1 Creating objects
conn = connection (parameter list)
2.1.1 Parameter list
- Host: Connected MySQL host, if this machine is ' localhost '
- Port: Ports of the connected MySQL host, default is 3306
- DB: Name of the database
- User: The username of the connection
- Password: the password for the connection
- CharSet: The encoding method used by the communication, the default is ' gb2312 ', the requirement is consistent with the encoding specified at the time of database creation, otherwise the Chinese will garbled the method of the object
ImportPymysql#get a database connectionConnection = Pymysql.connect (host='192.168.242.128', port=3306,user='Root', password='123456', db='test1', charset=' UTF8')#gets the object that interacts with the databasecursor =connection.cursor ()#the SQL to executesql ='SELECT * from emp'#get the data after sSQL executioncursor.execute (SQL) result=Cursor.fetchall () forResinchResult:Print(RES)
Python's interaction with MySQL