标签:python
安装mysql拓展
yum install python-devel pip install MySQL-python
2.在mysql中创建库
create database reboot10 default character set utf8;
3.创建表
create table users( id int AUTO_INCREMENT primary key ,name varchar(20) not null comment ‘用户名‘ ,name_cn varchar(50) not null comment ‘中文名‘ ,password varchar(50) not null comment ‘用户密码‘ ,email varchar(50) comment ‘电子邮件‘ ,mobile varchar(11) not null comment ‘手机号码‘ ,role varchar(10) not null comment ‘1:sa;2:php;3:ios;4:test‘ ,status tinyint ,create_time datetime comment ‘创建时间‘ ,last_time datetime comment ‘最后登录时间‘ ,unique key name(name))engine=innodb comment=‘用户表‘;
4.在python中操作mysql
>>> 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","1111@reboot.com","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‘1111@reboot.com‘, u‘12121212‘, u‘sa‘, 0, datetime.datetime(2016, 8, 6, 0, 0), datetime.datetime(2016, 8, 6, 0, 0)),) >>> db.commit() #提交 >>> 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 | 1111@reboot.com | 12121212 | sa | 0 | 2016-08-06 00:00:00 | 2016-08-06 00:00:00 | +----+------+---------+----------+-----------------+----------+------+--------+---------------------+---------------------+ 1 row in set (0.00 sec)
本文出自 “不抛弃!不放弃” 博客,谢绝转载!
标签:python
原文地址:http://thedream.blog.51cto.com/6427769/1838661