设为首页 加入收藏

TOP

python操作mysql
2017-09-30 17:36:57 】 浏览:4738
Tags:python 操作 mysql

1.安装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学习笔记enumerate()与rang.. 下一篇mysql-databaseython 3.4.0 with ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目