Python operations on the MySQL database

Source: Internet
Author: User
Tags mysql in

In Python, you can use the MySQLdb module to connect to the MySQL database and manipulate the MySQL database


" First step " MySQL Installation

Reference Documentation: http://blog.csdn.net/Jerry_1126/article/details/20837397


the second step connects to the MySQL

    • Create a database

Welcome to the MySQL Monitor.  Commands End With; or \g.your MySQL connection ID is 5Server version:5.5.19 mysql Community Server (GPL) mysql> #创建数据库mysql > Create data Base python; Query OK, 1 row affected (0.09 sec) #使用数据库mysql > Use python;database changed# Creating tables mysql> CREATE table people (name Varc HAR (+), age INT, Sex CHAR (1)); Query OK, 0 rows affected (0.44 sec) #插入数据mysql > insert into people values (' Tom ', ' M '); Query OK, 1 row affected (0.13 sec) mysql> insert into people values (' Jack ', null, NULL); Query OK, 1 row affected (0.06 sec) #查看数据mysql > select * FROM people;+------+------+------+| name | Age  | Sex  |+------+------+------+| Tom  |   20 | M    | | Jack | NULL | NULL |+------+------+------+2 rows in Set (0.05 sec)
    • Installing MYSQLDB

Official website: Http://sourceforge.net/projects/mysql-python

Download the exe file that matches your operating system, Python version, and click Next to complete the installation. As below, it means the installation was successful!

    • Using MySQL in Python

Import MySQLdb # Imports MYSQLDB Module DB = mysqldb.connect (host = ' localhost ',                     # Connect to the database, the server is native user = ' root ', # Username: root passwd = ' 1234 ', # Password: 1234 db = ' python ') # data Library: Pythoncur = Db.cursor () # Gets the database cursor Cur.execute (' Insert in   To people values ("Jee", +, "F") ') # Execute SQL, add record res = Cur.execute (' Delete from people where age=20 ') # Execute SQL, delete record                  Db.commit () # COMMIT TRANSACTION res = Cur.execute (' SELECT * from people ')                                                 # Execute SQL, get record res = Cur.fetchall () # Get all data print (RES)                                             # Print Data cur.close () # Close cursor Db.close ()    # Close the database connection  


For specific API definitions, please refer to:

http://blog.csdn.net/jerry_1126/article/details/40037899



Python operations on the MySQL database

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.