首先是安装工具

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. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  2. 报错:Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?

    原因分析:在StoryBoard中没有一个view controller设置了Initial Scene. 解决方案:在Storyboard中,选择一个view conroller作为story bo ...

  3. 搭建Android底层开发环境

    为了开发linux驱动方便些,我们一般将linux作为Android的开发环境,那么就需要搭建Android的开发环境,下面是一些搭建Android底层时的心得: (1)安装JDK:除了普遍使用的下载 ...

  4. paper 114:Mahalanobis Distance(马氏距离)

    (from:http://en.wikipedia.org/wiki/Mahalanobis_distance) Mahalanobis distance In statistics, Mahalan ...

  5. mysql 设置max_allowed_packet 大小的办法

        show VARIABLES like '%max_allowed_packet%'; 第一句是查询  max_allowed_packet  的大小,第二句是重新设定  max_allowe ...

  6. XAF 如何基于业务规则禁用属性

    // Developer Express Code Central Example: // How to: Disable Property Editors Based on a Business R ...

  7. qt qml qchart 图表组件

    qt qml qchart 图表组件 * Author: Julien Wintz * Created: Thu Feb 13 23:41:59 2014 (+0100) 这玩意是从chart.js迁 ...

  8. Java8-Function使用及Groovy闭包的代码示例

    导航 定位 概述 代码示例 Java-Function Groovy闭包 定位 本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场 ...

  9. IOS响应式编程框架ReactiveCocoa(RAC)使用示例

    ReactiveCocoa是响应式编程(FRP)在iOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...

  10. Centos7下用命令同步标准时间

    新装的CentO7S系统服务器可能设置了错误的时间,需要调整时区和时间.如下是CentOS7系统使用NTP协议,从一个时间服务器同步标准时间: [root@localhost ~]# cp /usr/ ...