Tutorial on configuring mysql in Python (must read)
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
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
Name | Source | Installation method |
The system comes with it | yum whatprovides python* Install the queried packages that match the keyword names one by one | |
The system comes with it | http://pypi.python.org/pypi/setuptools | Download, select the corresponding setuptools according to the python version version or use the built-in package yum install python-setuptools |
Download from the Internet |
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
[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 >>>
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]#
#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()
[root@localhost python]# python mysql-conn.py the user table content is: ('%', 'root', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', 0L, 0L, 0L, 0L, <br>'mysql_native_password', '123456', 'Y', datetime.datetime(2017, 9, 14, 14, 40, 2), None, 'N') [root@localhost python]#
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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 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 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 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.

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...

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