Introduction to the actual database connection Solution Using Python

Source: Internet
Author: User

Python is a commonly used computer language. In actual operations, we will involve the actual operations related to connecting Python to the database. If you are interested in or have doubts about the actual operation solution for connecting Python to the database, you can browse the following articles.

1. Connection Between Python and mysql Databases

To connect Python to MySQL, you can use the MySQL_python module to first determine whether to install python. In the command mode, import Python and then start checking:

Python 2.5.1 (r5.1: 54863, May 2 2007, 16:56:35)

 
 
  1. [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2  
  2. Type "help", "copyright", "credits" or "license" 
    for more information.  
  3. >>> import MySQLdb  
  4. Traceback (most recent call last):  
  5. File "", line 1, in  
  6. ImportError: No module named MySQLdb  
  7. >>> exit() 

If you see the preceding

 
 
  1. "ImportError: No module named MySQLdb" 

Note: In shell, the output is case-sensitive. You can also run the following commands to test your Database Configuration:

 
 
  1. >>> from django.db import connection >>>
     cursor = connection.cursor() 

If no error message is displayed, your database configuration is correct. Otherwise, you must check the error information to correct the error. The above code is an example of the code before connecting Python to the mysql database.

Install the mysql_python Module

Download and install MySQLdb on the official website

MySQLdb version:

 
 
  1. (win)MySQL-python-1.2.2.win32-py2.6.exe 

Directly run the installation and follow the steps below to install

 
 
  1. $ tar zxvf MySQL-python-1.2.2.tar.gz  
  2. $ cd MySQL-python-1.2.2  
  3. $ python setup.py build  
  4. $ python setup.py install 

Attachment:

 
 
  1. libguide40.dll.zip(77.3 KB)  
  2. libmmd.dll.zip(169 KB)  
  3. libmySQL.dll.zip(861 KB) 

The above is an introduction to the connection between python and mysql databases in the python database. I hope you will gain some benefits.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.