-
安装mysql拓展
1
2
|
yum
install
python-devel
pip
install
MySQL-python
|
2.在mysql中创建库
1
|
create
database
reboot10
default
character
set
utf8;
|
3.创建表
1
2
3
4
5
6
7
8
9
10
11
12
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>>
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()
|
1
2
3
4
5
6
7
|
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)
|
本文转自 shouhou2581314 51CTO博客,原文链接:http://blog.51cto.com/thedream/1838661,如需转载请自行联系原作者