In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to operate the MySQL database in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Python standard database interface provides PythonDB-API,PythonDB-API with database application programming interface for developers.
The Python database interface supports a large number of databases. You can choose the database that is suitable for your project:
GadFly
MSQL
MySQL
PostgreSQL
MicrosoftSQLServer2000
Informix
Interbase
Oracle
Sybase
You can visit the Python database interface and API to view a detailed list of supported databases.
Different databases you need to download different DBAPI modules, for example, 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 a variety of underlying database systems and a variety of database interface programs.
The DB-API of Python implements the interface for most databases. After connecting to each database, you can operate each database in the same way.
PythonDB-API usage process:
The API module is introduced.
Gets the connection to the database.
Execute SQL statements and stored procedures.
Close the database connection.
What is MySQLdb?
MySQLdb is an interface for Python to link to Mysql databases. It implements Python database API specification V2.0, which is based on MySQLCAPI.
How do I install MySQLdb?
In order to write MySQL scripts in DB-API, you must make sure that MySQL is installed. Copy the following code and execute:
#! / usr/bin/python
#-*-coding:UTF-8-*-
ImportMySQLdb
If the output after execution is as follows, it means that you do not have the MySQLdb module installed:
Traceback (mostrecentcalllast):
File "test.py", line3,in
ImportMySQLdb
ImportError:NomodulenamedMySQLdb
To install MySQLdb, please visit http://sourceforge.net/projects/mysql-python. (the Linux platform can visit: https://pypi.python.org/pypi/MySQL-python) can choose the installation package suitable for your platform, divided into precompiled binaries and source code installation packages.
If you choose the binary distribution, the installation process can be completed with basic installation prompts. If you install from source code, you need to change to the top-level directory of the MySQLdb distribution and type the following command:
$gunzipMySQL-python-1.2.2.tar.gz
$tar-xvfMySQL-python-1.2.2.tar
$cdMySQL-python-1.2.2
$pythonsetup.pybuild
$pythonsetup.pyinstall
Note: please make sure that you have root permission to install the above modules.
Database connection
Before connecting to the database, confirm the following:
You have created the database TESTDB. Exe.
You have created the table EMPLOYEE in the TESTDB database
The EMPLOYEE table fields are FIRST_NAME,LAST_NAME,AGE,SEX and INCOME.
Connect to the database TESTDB using the user name "testuser", the password is "test123", you can set or directly use the root user name and password, Mysql database user authorization, please use the Grant command.
The PythonMySQLdb module has been installed on your machine.
After reading the above, do you have any further understanding of how to operate the MySQL database in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.