Python's interaction with MySQL

Source: Internet
Author: User
Tags mysql host

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

    1. Host: Connected MySQL host, if this machine is ' localhost '
    2. Port: Ports of the connected MySQL host, default is 3306
    3. DB: Name of the database
    4. User: The username of the connection
    5. Password: the password for the connection
    6. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.