Home Backend Development Python Tutorial Tutorial on configuring mysql in Python (must read)

Tutorial on configuring mysql in Python (must read)

Oct 13, 2017 am 10:57 AM
mysql python

The editor below will bring you a tutorial on configuring mysql in Python (recommended). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look.

Linux system comes with Python, and configure mysql for python according to the system’s own resources; installation requires that the correct yum source has been configured;

In python When mysql is not configured, the prompt for directly importing MySQLdb is as follows


 >>> import MySQLdb
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ImportError: No module named MySQLdb
Copy after login

There is no rpm installation package for mysql-python in the Linux system. This resource needs to be downloaded from the Internet:

https://sourceforge.net/projects/mysql-python

Currently the mainstream uses Python2.6 or Python2.7 version, download it as MySQL-python-1.2.3c1 .tar.gz

After downloading, upload it to the Linux machine and put it in the non-Chinese directory

##tar -xf MySQL -python-1.2.3c1.tar.gz, the decompression directory is as follows


[root@localhost home]# cd MySQL-python-1.2.3c1/
[root@localhost MySQL-python-1.2.3c1]# ll
总用量 240
drwxr-xr-x. 5 root root  89 10月 12 12:27 build
-rw-r--r--. 1 tianF enosoft 59580 3月 31 2009 ChangeLog
drwxr-xr-x. 2 root root  57 10月 12 12:27 dist
drwxr-xr-x. 2 tianF enosoft 58 3月 31 2009 doc
-rw-r--r--. 1 tianF enosoft 9716 2月 6 2009 ez_setup.py
-rw-r--r--. 1 tianF enosoft 17989 2月 25 2007 GPL
-rw-r--r--. 1 tianF enosoft 2935 3月 4 2007 HISTORY
-rw-r--r--. 1 tianF enosoft 605 2月 11 2007 MANIFEST
-rw-r--r--. 1 tianF enosoft 272 3月 9 2009 MANIFEST.in
-rw-r--r--. 1 tianF enosoft 2098 3月 31 2009 metadata.cfg
-rw-r--r--. 1 tianF enosoft 75431 3月 31 2009 _mysql.c
drwxr-xr-x. 3 tianF enosoft 211 10月 12 12:28 MySQLdb
-rw-r--r--. 1 tianF enosoft 2306 4月 5 2006 _mysql_exceptions.py
-rw-r--r--. 1 root root  3791 10月 12 12:28 _mysql_exceptions.pyc
drwxr-xr-x. 2 tianF enosoft 90 3月 31 2009 MySQL_python.egg-info
-rw-r--r--. 1 tianF enosoft 1755 3月 31 2009 PKG-INFO
-rw-r--r--. 1 tianF enosoft 3203 4月 5 2006 pymemcompat.h
-rw-r--r--. 1 tianF enosoft 6696 10月 17 2008 README
-rw-r--r--. 1 tianF enosoft 380 3月 31 2009 setup.cfg
-rw-r--r--. 1 tianF enosoft 951 3月 8 2009 setup_common.py
-rw-r--r--. 1 root root  1520 10月 12 12:27 setup_common.pyc
-rw-r--r--. 1 tianF enosoft 2947 3月 8 2009 setup_posix.py
-rw-r--r--. 1 root root  2977 10月 12 12:27 setup_posix.pyc
-rw-r--r--. 1 tianF enosoft 495 10月 18 2008 setup.py
-rw-r--r--. 1 tianF enosoft 1547 3月 4 2007 setup_windows.py
-rw-r--r--. 1 tianF enosoft 592 10月 17 2008 site.cfg
drwxr-xr-x. 2 tianF enosoft 149 3月 31 2009 tests
Copy after login

Before configuring python-mysql, also Some dependencies need to be installed; otherwise various missing errors will be reported

python-develThe system comes with itmysql-servermysql-develsetuptoolsThe system comes with ithttp://pypi.python.org/pypi/setuptoolsMySQL-pythonDownload from the Internet
NameSource Installation method
yum whatprovides python*

Install the queried packages that match the keyword names one by one

Download, select the corresponding setuptools according to the python version version or use the built-in package yum install python-setuptools

You can use the yum whatprovides mysql-devel command to check whether it has been installed (the package name under the old version of Linux system is mysql-dev, if mysql-devel prompts that it cannot be found, use dev instead):

If the command reports an error, it means that the yum source configuration is incorrect, or the installation CD does not match the system, etc. For details, please refer to the yum source configuration tutorial under Linux


[root@localhost mysql-python]# <strong>yum whatprovides mysql-devel</strong>
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base        | 3.9 kB  00:00 ...
mysql-devel-5.1.66-2.el6_3.x86_64 : Files for development of MySQL applications
Repo  : base
Matched from:
 
mysql-devel-5.1.66-2.el6_3.i686 : Files for development of MySQL applications
Repo  : base
Matched from:
 
mysql-devel-5.1.66-2.el6_3.x86_64 : Files for development of MySQL applications
Repo  : installed
Matched from:
Other  : Provides-match: mysql-devel
 
mysql-devel-5.1.66-2.el6_3.i686 : Files for development of MySQL applications
Repo  : installed
Matched from:
Other  : Provides-match: mysql-devel
Copy after login

As shown above, if the Repo value is installed, it means it has been installed; mainly observe whether the package of the mysql-devel keyword has been installed; if not, enter the yum install mysql-devel command to install it

Install mysql-devel, python-devel, python-setuptools in sequence, and the installation process will continue if no errors are reported;

After the installation of the above dependencies is completed, return to MySQL-python-1.2.3c1 extracted from MySQL-python /directory;

>> python setup.py build

>> python setup.py install

If the above two commands are executed correctly, it means python configuration mysqldb is successful, verify again whether an error is reported when importing MySQLdb


[root@localhost mysql-python]#
[root@localhost mysql-python]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
Copy after login

The above indicates that the configuration of MySQLdb is successful

Common errors: python setup.py build Prompt that mysql_config cannot be found

This problem is because when building mysqldb, the mysql_config configured in the site.cfg file in the MySQL-python-1.2.3c1/ directory is used;


[root@localhost MySQL-python-1.2.3c1]# ls
build  doc   HISTORY  metadata.cfg _mysql_exceptions.py PKG-INFO  setup.cfg   setup_posix.py setup_windows.py
ChangeLog ez_setup.py MANIFEST  _mysql.c  _mysql_exceptions.pyc pymemcompat.h setup_common.py setup_posix.pyc site.cfg
dist  GPL   MANIFEST.in MySQLdb  MySQL_python.egg-info README   setup_common.pyc setup.py   tests
[root@localhost MySQL-python-1.2.3c1]# more site.cfg
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)
 
embedded = False
threadsafe = True
static = False
 
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
#mysql_config = /usr/local/bin/mysql_config
 
# The Windows registry key for MySQL.
# This has to be set for Windows builds to work.
# Only change this if you have a different version.
registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0
[root@localhost MySQL-python-1.2.3c1]#
Copy after login

If the installation location of mysql does not match the location configured in site.cfg, you need to modify the site.cfg file

#mysql_config Configure, uncomment the previous comment, and configure it to the correct address. For example

mysql_config = /usr/bin/mysql_config #(If not specified, the location of mysql_config defaults to the /usr/bin directory, and there are differences in different systems. , the actual location can be obtained by searching the file)

Verify the python-Mysql function

Configure the Mysql database as needed and modify the user name and password;

View the basic syntax of python-mysql, link the mysql library of the mysql database, and obtain the user table information. The code is as follows;


#!/usr/bin/python
#encoding=utf8
 
import MySQLdb
conn=MySQLdb.connect("127.0.0.1","root","123456","mysql")
cursor=conn.cursor()
cursor.execute("select * from user")
getdata=cursor.fetchone()
print "the user table content is:",getdata
conn.close()
Copy after login

The execution results are as follows :


[root@localhost python]# python mysql-conn.py
the user table content is: (&#39;%&#39;, &#39;root&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, 0L, 0L, 0L, 0L, <br>&#39;mysql_native_password&#39;, &#39;123456&#39;, &#39;Y&#39;, datetime.datetime(2017, 9, 14, 14, 40, 2), None, &#39;N&#39;)
[root@localhost python]#
Copy after login
At this point, the Python configuration Mysql verification has passed!

The above is the detailed content of Tutorial on configuring mysql in Python (must read). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Does Python projects need to be layered? Does Python projects need to be layered? Apr 19, 2025 pm 10:06 PM

Discussion on Hierarchical Structure in Python Projects In the process of learning Python, many beginners will come into contact with some open source projects, especially projects using the Django framework...

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

See all articles