首先是安装工具

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的操作:
查询
 #!/usr/bin/env python
#-*- encoding: utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='127.0.0.1', user = 'root',passwd ='',db='yangshanlei') #连接mysql
cur = conn.cursor() #创建游标 reCount = cur.execute('select * from students') #查询sql语句
data = cur.fetchall() #把得到的数据都拿出来 cur.close() #关闭游标
conn.close() #关闭连接 print reCount #查询出影响行数
print data

查询

insert

 #!/usr/bin/env python
#-*- encoding: utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='127.0.0.1', user = 'root',passwd ='',db='yangshanlei') #连接mysql
cur = conn.cursor() #创建游标 sql = "insert into students (name,sex,age,tel) values(%s,%s,%s,%s)"
params = ('yang','man',19,'') reCount = cur.execute(sql,params)
conn.commit() #insert update delete都需要加上commit cur.close() #关闭游标
conn.close() #关闭连接 print reCount #查询出影响行数

插入一行

 #!/usr/bin/env python
#-*- encoding: utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='127.0.0.1', user = 'root',passwd ='',db='yangshanlei') #连接mysql
cur = conn.cursor() #创建游标 li =[
('www','usa',19,''),
('sss','usa',19,'')
] reCount = cur.executemany("insert into students (name,sex,age,tel) values(%s,%s,%s,%s)",li) conn.commit() #insert update delete都需要加上commit
cur.close() #关闭游标
conn.close() #关闭连接 print reCount #查询出影响行数

插入多行

delete

 #!/usr/bin/env python
#-*- encoding: utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='127.0.0.1', user = 'root',passwd ='',db='yangshanlei') #连接mysql
cur = conn.cursor() #创建游标 sql = "delete from students where name =%s"
params = ('test1',) reCount = cur.execute(sql,params)
conn.commit() #insert update delete都需要加上commit cur.close() #关闭游标
conn.close() #关闭连接 print reCount #查询出影响行数

delete

update

 #!/usr/bin/env python
#-*- encoding: utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='127.0.0.1', user = 'root',passwd ='',db='yangshanlei') #连接mysql
cur = conn.cursor() #创建游标 sql = "update students set name = %s where id = 1"
params = ('sb',) reCount = cur.execute(sql,params)
conn.commit() #insert update delete都需要加上commit cur.close() #关闭游标
conn.close() #关闭连接 print reCount #查询出影响行数

update

 #!/usr/bin/env python
#-*- encoding: utf- -*-
import MySQLdb class helperall(object):
def __init__(self):
pass def Get_Dict(self,sql,params):
#conn = MySQLdb.connect('127.0.0.1','root','','yangshanlei')
conn = MySQLdb.connect(host='127.0.0.1', user = 'root',passwd ='',db='yangshanlei')
cur = conn.cursor() reCount = cur.execute(sql,params)
date = cur.fetchall() cur.close()
conn.close() return date def Get_One(self,sql,params):
conn = MySQLdb.connect('127.0.0.1','root','','yangshanlei')
cur = conn.cursor() #打开游标 reCount = cur.execute(sql,params)
data = cur.fetchone() cur.close()
conn.close()
return data def Get_insertone(self,sql,params):
conn = MySQLdb.connect('127.0.0.1','root','','yangshanlei')
cur = conn.cursor() reCount = cur.execute(sql,params)
conn.commit() cur.close()
conn.close()
return reCount def Get_insertall(self,sql,li):
conn = MySQLdb.connect('127.0.0.1','root','','yangshanlei')
cur = conn.cursor() reCount = cur.executemany(sql,li) conn.commit() #insert update delete都需要加上commit
cur.close()
conn.close() '''
#插入多次
helper = helperall()
li =[
('ww1','usa'),
('ss1','usa')
]
sql = "insert into students (name,sex) values(%s,%s)"
dicct_insertall = helper.Get_insertall(sql,li)
print dicct_insertall
'''
'''
#插入一次
helper = helperall()
sql = "insert into students (name,sex) values(%s,%s)"
params = ('yangshan','man')
dicct_insert = helper.Get_insertone(sql,params)
print dicct_insert
'''
'''
#查询
helper = helperall()
sql = "select * from students where id > %s"
params = (,)
dict_data = helper.Get_Dict(sql,params) #查询全部
dict_one = helper.Get_One(sql,params) #查询1次
print dict_data
print dict_one
'''
												

python-05的更多相关文章

  1. 【Python 05】Python开发环境搭建

    Python3安装和使用 1.安装 Python管方下载地址 选择Customize installation安装,并且勾选Add Python 3.X to PATH. 勾选Documentatio ...

  2. headfirst python 05, 06

    处理数据 with open('james.txt') as jaf: data = jaf.readLine() james = data.strip().split(',') #先去掉空格而否有, ...

  3. [Python] 05 - Load data from Files

    文件读写 一.文件打开 传统方法 >>> f = open('data.txt', 'w') # Make a new file in output mode ('w' is wri ...

  4. python 05 关于对python中引用的理解

    数据的在内存中的地址就是数据的引用. 如果两个变量为同一个引用,那么这两个变量对应的数据一定相同: 如果两个变量对应的数据相同,引用不一定相同. 通过id(数据)可以查看数据对应的地址,修改变量的值, ...

  5. Python 05 Geany的基本使用1

    问题01:代码中包含中文编译时提示错误 原文:https://blog.csdn.net/weixin_43345286/article/details/82951698 解决:文档 - 设置文件编码 ...

  6. python 05—字典

    一.字典的键是唯一的 键:简单对象,例[字符串.整数.浮点数.bool值] list不能作为键,但可以作为值. 例: score = { '萧峰' : 95, '段誉' : 97, '虚竹' : 89 ...

  7. python 05 列表 元组 (序列)

    循环(loop),指的是在满足条件的情况下,重复执行同一段代码.比如,while语句. [ 循环则技能对应集合,列表,数组等,也能对执行代码进行操作.] 迭代(iterate),指的是按照某种顺序逐个 ...

  8. 实验与作业(Python)-05 程序的控制结构

    推荐完成顺序: 1->2->3->4.1->4.4->5->4.5->4.7->6 截止日期 下次实验课之前 实验目标 if-elif-else 循环: ...

  9. python 05集合

    1.集合 特性:可变的,不同元素组成,无序,集合中元素类型必须是不可变(数字,元组,字符串) 形式:s={1,"good",(2,3)} 方法:add(), clear()清空, ...

  10. python --- 05 字典 集合

    一.字典 可变数据类型 {key:value}形式   查找效率高   key值必须是不可变的数据类型 1.增删改查 1).增    dic["新key"] = "新va ...

随机推荐

  1. 多线程中共享变量——CCF总决赛试题

    题目要求 数据格式 Q 系统的输入为纯文本格式的文件,由若干行组成,每一行由城市编号.年龄.收入组成,相邻两项之间用一个空格分隔.以下是输入的一个片段: 1001 20 12000 1001 50 2 ...

  2. Qt设计器中设置border-image注意问题

    Qt版本4.8.5,Windows7操作系统,在Qt设计师中通过样式表设计设置widget的border-image属性,debug版本编译会正常显示,但是release版本编译运行就不显示,把图片格 ...

  3. 《zw版·Halcon-delphi系列原创教程》 zw版-Halcon常用函数Top100中文速查手册

    <zw版·Halcon-delphi系列原创教程> zw版-Halcon常用函数Top100中文速查手册 Halcon函数库非常庞大,v11版有1900多个算子(函数). 这个Top版,对 ...

  4. Oracle表结构修改触发视图无法正常使用问题

    一.问题描述 当对视图使用的基表进行表结构修改后,会触发视图的无效以及编译出错问题,必须重建视图解决. 二.问题再现 1.Oracle10g环境 1.1 创建视图测试用两张基表:TestTable和T ...

  5. MAC OS UI设计

    对比MAC OS 10.9到10.11的UI设计,苹果曾经卓尔不群的审美观逐步变得泯然众人. 当苹果也跟上扁平化的浪潮,许多搞设计的朋友都一时难以接受,曾经潮流的引领者变成了亦步亦趋的跟随者. MAC ...

  6. 制作centos的U盘启动盘

    制作centos的U盘启动盘比ubuntu麻烦一些,因为可能涉及到fat32文件格式不支持大于4G的文件存储的问题,而最新版本的centos就是大于4G的,所以就需要对U盘进行分区. 一个做主引导,一 ...

  7. CRM 2013 限制Lookup

    var oTo = document.getElementById("customerid_i"); oTo.setAttribute("defaulttype" ...

  8. iOS 图片的按照比例拉伸

    // 这是一个UIImage 的分类 (  UIImage+Extension.h   ) UIImage+Extension.h #import <UIKit/UIKit.h> @int ...

  9. 一个多重阴影的DIV框框

    要绝对定位,而且需要设置z-index,垂直高度,数值越小越看不见. <div class="beauty">I am your girl~</div> & ...

  10. Android Bootloader LittleKernel的两篇文章 【转】

    转自:http://blog.csdn.net/loongembedded/article/details/41747523 2014-12-05 14:37 3599人阅读 评论(2) 收藏 举报 ...