学mysql需要学python吗_python 学习 第六篇 mysql

安装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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值