Home  >  Article  >  Backend Development  >  An example of using python to operate mysql

An example of using python to operate mysql

零下一度
零下一度Original
2017-07-18 15:12:001374browse

1. Python’s standard database interface is Python DB-API, which provides developers with a database application programming interface.

The Python database interface supports many databases. You can choose the database that suits your project:

GadFly

mSQL

MySQL

PostgreSQL

Microsoft SQL Server 2000

Informix

Interbase

Oracle

Sybase

you You can visit the Python database interface and API to view a detailed list of supported databases.

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 to provide a consistent access interface for various underlying database systems and various database interface programs. .

Python's DB-API implements interfaces 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 the API module.

Get the connection to the database.

Execute SQL statements and stored procedures.

Close the database connection.

2. How to install MySQLdb?

In order to write MySQL scripts using DB-API, you must ensure that MySQL has been installed. Copy the following code and execute it:

#!/usr/bin/python# -*- coding: UTF-8 -*-import MySQLdb

If the output after execution As shown below, it means that you have not installed the MySQLdb module:

Traceback (most recent call last):
File "test.py", line 3, in <module>
MySQLdbImportError: No module named MySQLdb

To install MySQLdb, please visit sourceforge.net/projects/mysql-python, (for Linux platform, you can visit: pypi.python.org/pypi/MySQL- python) From here you can choose the installation package suitable for your platform, which is divided into precompiled binary files and source code installation packages.

If you choose the binary file release version, the installation process will be completed with basic installation prompts. If you are installing from source code, you need to switch to the top-level directory of the MySQLdb distribution and type the following command:

$ gunzip MySQL-python-1.2.2.tar.gz
$ tar -xvf MySQL-python-1.2.2.tar$ cd MySQL-python-1.2.2$ python setup.py build
$ python setup.py install

Note: Please make sure you have root permissions to install the above modules.

The above is the detailed content of An example of using python to operate mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn