Python operation MySQL

Source: Internet
Author: User

1. Install MySQL extension

Yum Install Python-develpip Install Mysql-python

  

2. Create a library in MySQL

Create DATABASE reboot10 default character set UTF8;

  

3. Create a table

CREATE table users (ID int auto_increment primary key,name varchar) NOT NULL comment ' user name ', NAME_CN varchar (NOT NULL) Comment ' Chinese name ', password varchar (not null comment ' user password ', email varchar () comment ' e-mail ', mobile varchar (one) NOT NULL CO Mment ' mobile number ', role varchar (+) NOT null comment ' 1:sa;2:php;3:ios;4:test ', status tinyint,create_time datetime comment ' Creation time ', last_time datetime comment ' Last login time ', unique key name (name) engine=innodb comment= ' User table ';

  

4. Working with MySQL in Python

>>> import MySQLdb as mysql>>> db=mysql.connect (user= ' root ', passwd= ' www.123 ', db= ' reboot10 ', charset= ' UTF8 ') >>> cur=db.cursor () >>> cur.execute (' SELECT * from users ') 0l>>> sql= ' Insert into users (Name,name_cn,password,email,mobile,role,status,create_time,last_time) VALUES ("WD", "Pcss", "123456", "[ Email protected] "," 12121212 "," sa ", 0," 20160806 "," 20160806 ") ' >>> cur.execute (SQL) 1l>>> Cur.execute (' SELECT * from users ') 1l>>> Cur.fetchall (                    (3L, U ' wd ', U ' pcss ', U ' 123456 ', u ' [email protected ] ', u ' 12121212 ', u ' sa ', 0, Datetime.datetime (8, 6, 0, 0), Datetime.datetime (8, 6, 0, 0)),) >>> Db.comm It ()  #提交 >>> cur.close () >>> db.close ()

  

Mysql> SELECT * FROM users;+----+------+---------+----------+-----------------+----------+------+--------+----- ----------------+---------------------+| ID | name | NAME_CN | password | Email           | mobile   | role | status | Create_time         | last_time           |+----+------+---------+----------+--------- --------+----------+------+--------+---------------------+---------------------+|  3 | WD   | pcss    | 123456   | [Email protected] | 12121212 | SA   |      0 | 2016-08-06 00:00:00 | 2016-08-06 00:00:00 |+----+------+---------+----------+-----------------+----------+------+--------+------------ ---------+---------------------+1 row in Set (0.00 sec)

  

Python operation MySQL

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.