首先是安装工具

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. easyUI 复选框批量操作

    前台js代码 function destroyExcelout(){ //返回选中多行              var row = $('#dg').datagrid('getSelections' ...

  2. 华盛顿大学 Programming Languages

    表达式三要素: 语法,类型和计算值.

  3. PB函数大全

    PB函数大全 Abs()功能计算绝对值.语法Abs ( n )参数n:要得到绝对值的数值型变量或表达式返回值返回值的数据类型与n的数据类型相同,函数执行成功时返回n的绝对值.如果参数n的值为NULL, ...

  4. java加法流程图

  5. MS CRM 2013 Plugin 注册工具登录后空白

    解决办法 把en-us, zh-CN 目录随便改个名字就好了

  6. node上截取图片工具 images(node-images)

    我们经常会遇到服务器上传的图片进行裁剪或者增加logo等等一些操作,在node平台上该如何实现呢? 看到大家都在使用"gm"这个工具,功能很强大,但是在Windows平台上简直就是 ...

  7. JavaScript,复习总结

    ECMA(European Computer Manufacturers Association)欧洲计算机制造商协会.其制定很多标准:C#语言规范:C++/CLI语言规范:Eiffel语言:CD-R ...

  8. next([expr]) 取得一个包含匹配的元素集合中每一个元素紧邻的后面同辈元素的元素集合。

    描述: 找到每个段落的后面紧邻的同辈元素. HTML 代码: <p>Hello</p><p>Hello Again</p><div>< ...

  9. get([index])

    get([index]) 概述 取得其中一个匹配的元素. num表示取得第几个匹配的元素.从0开始,返回的是DOM对象,类似的有eq(index),不过eq(index)返回的是jQuery对象. 这 ...

  10. Linux-wget

    Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...