首页 > 编程语言 > 详细

python-05

时间:2017-01-04 07:19:24      阅读:165      评论:0      收藏:0      [点我收藏+]

首先是安装工具

Linux

  • 安装mysql:mysql-server
  • 安装python-mysql模块: python-mysqldb

Windows

  • 下载安装mysql
  • python操作mysql模块:MySQL-python-1.2.3.win32-py2.7.exe 或 MySQL-python-1.2.3.win-amd64-py2.7.exe
  • mysql图形界面:Navicat_for_MySQL

 

创建数据库

create table students
    (
        id int  not null auto_increment primary key,
        name char(8) not null,
        sex char(4) not null,
        age tinyint unsigned not null,
        tel char(13) null default "-"
    );

插入一条数据: insert into student (name,sex,age,tel) values(‘test‘,‘man‘,19,‘123456777‘)


MySQLdb的操作:
查询
技术分享
 1 #!/usr/bin/env python
 2 #-*- encoding: utf-8 -*-
 3 
 4 import MySQLdb
 5 
 6 conn = MySQLdb.connect(host=127.0.0.1, user = root,passwd =123456,db=yangshanlei)  #连接mysql
 7 cur = conn.cursor()   #创建游标
 8 
 9 reCount = cur.execute(select * from students)   #查询sql语句
10 data = cur.fetchall()     #把得到的数据都拿出来
11  
12 
13 cur.close()   #关闭游标
14 conn.close()  #关闭连接
15 
16 print reCount    #查询出影响行数
17 print data
查询

 insert

技术分享
 1 #!/usr/bin/env python
 2 #-*- encoding: utf-8 -*-
 3 
 4 import MySQLdb
 5 
 6 conn = MySQLdb.connect(host=127.0.0.1, user = root,passwd =123456,db=yangshanlei)  #连接mysql
 7 cur = conn.cursor()   #创建游标
 8 
 9 sql = "insert into students  (name,sex,age,tel) values(%s,%s,%s,%s)"
10 params = (yang,man,19,2222222)    
11 
12 
13 reCount = cur.execute(sql,params)
14 conn.commit()        #insert update delete都需要加上commit
15 
16 cur.close()   #关闭游标
17 conn.close()  #关闭连接
18 
19 print reCount    #查询出影响行数
插入一行
技术分享
 1 #!/usr/bin/env python
 2 #-*- encoding: utf-8 -*-
 3 
 4 import MySQLdb
 5 
 6 conn = MySQLdb.connect(host=127.0.0.1, user = root,passwd =123456,db=yangshanlei)  #连接mysql
 7 cur = conn.cursor()   #创建游标
 8  
 9 li =[
10      (www,usa,19,555),
11      (sss,usa,19,666)
12      ]
13 
14 reCount = cur.executemany("insert into students (name,sex,age,tel) values(%s,%s,%s,%s)",li)  
15 
16 conn.commit()        #insert update delete都需要加上commit
17 cur.close()   #关闭游标
18 conn.close()  #关闭连接
19 
20 print reCount    #查询出影响行数
插入多行

 

delete

技术分享
 1 #!/usr/bin/env python
 2 #-*- encoding: utf-8 -*-
 3 
 4 import MySQLdb
 5 
 6 conn = MySQLdb.connect(host=127.0.0.1, user = root,passwd =123456,db=yangshanlei)  #连接mysql
 7 cur = conn.cursor()   #创建游标
 8 
 9 sql = "delete from students where name =%s"
10 params = (test1,)    
11 
12 
13 reCount = cur.execute(sql,params)
14 conn.commit()        #insert update delete都需要加上commit
15 
16 cur.close()   #关闭游标
17 conn.close()  #关闭连接
18 
19 print reCount    #查询出影响行数
delete

update

技术分享
 1 #!/usr/bin/env python
 2 #-*- encoding: utf-8 -*-
 3 
 4 import MySQLdb
 5 
 6 conn = MySQLdb.connect(host=127.0.0.1, user = root,passwd =123456,db=yangshanlei)  #连接mysql
 7 cur = conn.cursor()   #创建游标
 8 
 9 sql = "update students set name  = %s  where id = 1"
10 params = (sb,)    
11 
12 
13 reCount = cur.execute(sql,params)
14 conn.commit()        #insert update delete都需要加上commit
15 
16 cur.close()   #关闭游标
17 conn.close()  #关闭连接
18 
19 print reCount    #查询出影响行数
update

 



python-05

原文:http://www.cnblogs.com/augustyang/p/6246926.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!