What are the methods for python to connect to mysql

The Python database interface supports a lot of databases, you can choose the database that suits your project:

GadFlymSQL MySQL PostgreSQL Microsoft SQL Server 2000 InformixInterbase Oracle Sybase

You need to download different DB API modules for different databases. For example, if you need to access Oracle database and Mysql data, you need to download Oracle and MySQL database modules.

DB-API is a specification. It defines a series of necessary objects and database access methods in order to provide a consistent access interface for various underlying database systems and various database interface programs.

Python's DB-API implements an interface for most databases. After using it to connect to each database, you can operate each database in the same way.

Python DB-API usage process:

Introduce API module. Get the connection to the database. Execute SQL statements and stored procedures. Close the database connection.

MySQLdb

MySQLdb is an interface for Python to link to Mysql database. It implements the Python database API specification V2.0 and is based on the MySQL C API.

installation

Use pip to install directly. Before that, you need to install some system dependencies.

CentOSyum install gcc python-devel mysql-devel zlib-devel openssl-devel
Ubuntusudo apt-get install libmysqlclient-dev libmysqld-dev python-dev python-setuptools

After installing the dependencies, use pip to install directly. The name of the MySQLdb module is called MySQL-python on pip.

Knowledge point expansion:

Use python to connect to mysql database

Six commonly used connection parameters

  1. Parameter host: the ip of the host where the mysql server is located;
  2. Parameters user: username
  3. Parameter password: password
  4. Parameter port: the port of the connected mysql host, the default is 3306
  5. Parameter db: the name of the connected database
  6. Parameter charset: When reading the data, there will be garbled Chinese characters, we need to set the encoding; when we use python to operate the database, then python is equivalent to the client, we use this client to operate the mysql server server, python3 uses it by default Utf8 character set, my mysql server uses latin1 character set by default, so every table created in mysql is encoded with utf8 when the table is built, so the encoding of the connection connector should be set here.

So far, this article on the ways to connect python to mysql is introduced. For more information about how to connect python to mysql, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future. !

Recommended Posts

What are the methods for python to connect to mysql
What are the ways to open files in python
​What are the numbers in Python?
What are the required parameters of python
What are the advantages of python language
Solution for Ubuntu unable to connect to the network
What conditions are needed for infinite loop in Python
What are python class attributes
What is the use of Python
How to view the python module
centos7 cannot connect to the external network
Python string to judge the password strength
What is the scope of python variables
What is the id function of python
Python | So collections are so easy to use! !
What are web development frameworks in python
Python novice learns to use the library
What system is good for self-study python
How to learn the Python time module
What is the prospect of python development
What is the function body of python
What is the function of adb in python
How to use the round function in python
Dry goods | Use Python to operate mysql database
How to use the zip function in Python
Python uses the email module to send mail
An article to understand the yield in Python
What does the tab key in python mean
How to use the format function in python
How to enter python through the command line
What is the difference between python and pycharm
How to switch the hosts file using python
How to install the downloaded module in python
Remotely connect to MySQL database in Ubuntu environment
Python how to move the code collectively right
The best way to judge the type in Python
Use python to realize the aircraft war game
How to install the latest MySQL on Ubuntu 18.04
Python generates the dependent files required for the project