How to Learn: How to Learn Python (2): how to install MySQLdb and how to connect to MySql through Python Testing

Source: Internet
Author: User
How to Learn: How to Learn Python (2): how to install MySQLdb and how to connect to MySqlEasy_install to install MySQLdb

It's so simple that I don't know what to say. One sentence.

  1. Sodu easy_install mysql-python

What if an error is reported?

If an error is reported, I can't help it either. I can install it smoothly here, and I can't demonstrate the error even though it works. However, it is said that there are many errors.

You need to believe that you have encountered all the mistakes you have encountered before, so feel free to use the search engine.

Install MySQLdb on Ubuntu

There is no explanation for the same command line.

  1. Sudo apt-get install python-mysqldb
Install MySQLdb in Windows

In fact, in essence, Windows Installation is complicated. I don't want to do anything about it. I just find a website based on your system. Download the corresponding version. Next step. I can't do anything else.

  1. Http://www.codegood.com/archives/129
Manually download and install MySQLdb

Compared with other methods, this is a complex and vast project.

First, we must first obtain the source code online!

The downloaded package is a compressed package. What should we do? decompress the package.

Go to the directory and find out where mysql_config is.

Edit site. cfg.

Modify mysql_config to the path found above.

Build

Install:

Do it manually.

Test

The test code is as follows:

  1. Import MySQLdb
  2.  
  3. Try:
  4. Conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = 'gaopeng', db = 'test', port = 3306)
  5. Cur = conn. cursor ()
  6. Cur.exe cute ("select version ()")
  7. Data = cur. fetchone ()
  8. Print "Database version: % s" % data
  9.  
  10.  
  11. Counter t MySQLdb. Error, e:
  12. Print "Mysql Error % d: % s" % (e. args [0], e. args [1])
  13.  
  14. Finally:
  15. If conn:
  16. Conn. close ()

The test results are as follows:

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.