Python operation MySQL Database

Source: Internet
Author: User

What is MySQLdb

MYSQLDB is the interface for the Python link MySQL database, which implements the Python database API specification V2.0, built on the MySQL C API.

How to install MySQLdb

install MYSQLDB, please visit   http://sourceforge.net/projects/mysql-python , (Linux platform can access: https://pypi.python.org/pypi/mysql-python) From here you can choose the installation package for your platform, divided into precompiled binaries and source code installation packages.

$ 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

Operation Syntax:

Import MySQLdb as MySQLdb

Con = mysqldb.connect (host = "localhost", user = "root", passwd= "123456", db = "test") #打开数据库

cur = db.cursor () #使用cursor () method gets the operation cursor

sql = select * from Ceshi #定义sql语句

Cur.execute (SQL) #使用execute方法执行SQL语句

data = Cur.fetchone () #使用fethcone () method gets a database

Cur.fetchall () Get all data

Con.commit () #提交到数据库

Con.rollback () #错误回滚

Con.close () #关闭数据库

Python operation MySQL database

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.