Connect MySQL database with Python

Source: Internet
Author: User

You must download a separate DB API module for each database you need to access. For example, if you need access to the Oracle database and MySQL database, you must download the Oracle and MySQL database modules.

The DB API provides the minimum standards for working with the database, using Python's structure and syntax whenever possible. This API includes the following:

    • Import the API module.

    • Gets the connection to the database.

    • Issue SQL statements and stored procedures.

    • Close connection

Verify that the MYSQLDB module is installed successfully:

#!/usr/bin/env python

Import MySQLdb

If it produces the following result, then it means that the MySQLdb module is not installed:

Traceback (most recent): File "test.py", line 3, Inimport mysqldbimporterror:no module named MySQLdb

Download MYSQLDB module, install command under Linux environment

$ gunzip mysql-python-1.2.2.tar.gz$ tar-xvf mysql-python-1.2.2.tar$ cd mysql-python-1.2.2$ python setup.py build$ python setup.py Install

* Note Root permissions Installation


Database connection:

Ensure the following before connecting to a MySQL database:

    • you have created a database testdb.

    • You have created an employee table in TestDB.

    • employee table has the following fields:  first_name, last_name, age, SEX and INCOME.

    • "testuser" User ID and password test12 "settings are used to access TestDB.

    • python module MYSQLDB properly installed on your machine.

    • You've passed the MySQL tutorial



Example 1: Connecting TestDB

#!/usr/bin/env pythonimport mysqldb# Open Database connectiondb = MySQLdb.connect ("Llocalhost", "TestUser", "test12", " TESTDB ") #prepare a Cursor object using the cursor () Methodcursor = Db.cursor () # Execute SQL Query using execute () methodcursor . Execute ("Select VERSION ()") #Fetch a single row using Fetch () Methoddata = Cursor.fetchone () print "Database version:%s"% Data#disconnect from Serverdb.close ()


While running this script on my Linux machine, it produces the following results.

Database version:5.0.45












This article is from the "Missed Footsteps" blog, please be sure to keep this source http://xxmspace.blog.51cto.com/1056016/1754124

Connect MySQL database with Python

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.