Python connects to MySQL database (Debian)

Source: Internet
Author: User
Tags mysql version

Python connects to MySQL database (Debian)

The following is a Python 2.* version of the installation method, Mysql-python temporarily does not support Python 3.* version

What to do in advance:
  1. Install Setuptools, run in terminal

    sudo python
  2. install pip, download "get_pip.py", run

     python get_pip.py  
  3. Run the following command to install the necessary packages

    sudo apt-get install python-dev libmysqld-dev libmysqlclient-dev
Official installation:
  1. install MySQL (some system comes with it):

     sudo  apt-get install mysql-server  
  2. download Mysql-python and unzip, go to the extracted folder, Run command

     python setup.py build python setup.py install  
    • Note : If you run the above command times wrong "Environmenterror:mysql_config not Found", rerun the following command to check if "Libmysqld-dev" is installed
      sudo apt-get install libmysqld-dev
Use

Code instance (from W3cschool), connect MySQL's testdb database, output MySQL version number:

# Encoding:utf-8#!/usr/bin/pythonImportMySQLdb# Open database connectiondb = MySQLdb.connect ("localhost","TestUser","test123","TESTDB")# Get an action cursor using the cursor () methodcursor = Db.cursor ()# Execute SQL statements using the Execute methodCursor.execute ("Select VERSION ()")# Use the Fetchone () method to get a database. data = Cursor.fetchone ()Print "Database version:%s"% data# Close the database connectionDb.close ()
?

Python connects to MySQL database (Debian)

Related Article

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.