python-05
首先是安装工具
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的更多相关文章
- 【Python 05】Python开发环境搭建
Python3安装和使用 1.安装 Python管方下载地址 选择Customize installation安装,并且勾选Add Python 3.X to PATH. 勾选Documentatio ...
- headfirst python 05, 06
处理数据 with open('james.txt') as jaf: data = jaf.readLine() james = data.strip().split(',') #先去掉空格而否有, ...
- [Python] 05 - Load data from Files
文件读写 一.文件打开 传统方法 >>> f = open('data.txt', 'w') # Make a new file in output mode ('w' is wri ...
- python 05 关于对python中引用的理解
数据的在内存中的地址就是数据的引用. 如果两个变量为同一个引用,那么这两个变量对应的数据一定相同: 如果两个变量对应的数据相同,引用不一定相同. 通过id(数据)可以查看数据对应的地址,修改变量的值, ...
- Python 05 Geany的基本使用1
问题01:代码中包含中文编译时提示错误 原文:https://blog.csdn.net/weixin_43345286/article/details/82951698 解决:文档 - 设置文件编码 ...
- python 05—字典
一.字典的键是唯一的 键:简单对象,例[字符串.整数.浮点数.bool值] list不能作为键,但可以作为值. 例: score = { '萧峰' : 95, '段誉' : 97, '虚竹' : 89 ...
- python 05 列表 元组 (序列)
循环(loop),指的是在满足条件的情况下,重复执行同一段代码.比如,while语句. [ 循环则技能对应集合,列表,数组等,也能对执行代码进行操作.] 迭代(iterate),指的是按照某种顺序逐个 ...
- 实验与作业(Python)-05 程序的控制结构
推荐完成顺序: 1->2->3->4.1->4.4->5->4.5->4.7->6 截止日期 下次实验课之前 实验目标 if-elif-else 循环: ...
- python 05集合
1.集合 特性:可变的,不同元素组成,无序,集合中元素类型必须是不可变(数字,元组,字符串) 形式:s={1,"good",(2,3)} 方法:add(), clear()清空, ...
- python --- 05 字典 集合
一.字典 可变数据类型 {key:value}形式 查找效率高 key值必须是不可变的数据类型 1.增删改查 1).增 dic["新key"] = "新va ...
随机推荐
- WebForm 内置对象
内置对象: Response对象:响应请求 Response.Write("<script>alert('添加成功!')</script>"); Respo ...
- mac的webdriver自动化
下载webdriver-chrome的连接:http://chromedriver.storage.googleapis.com/index.html
- JSP中动态INCLUDE与静态INCLUDE的区别
动态INCLUDE 用法: <jsp:include page="included.jsp" flush="true" /> 说明: 它总是会检查所 ...
- arrayToJson将数组转化为json格式的js代码 ///////////////////////zzzzzzzzzzzzzzzz
//去除空格 function trim(str) { return str.replace(/\s|\xA0/g,""); } /** *js数组转json * */ fun ...
- java.sql.SQLException: ORA-00942: 表或视图不存在
1.检查JDBC数据源是否配置正确:2.检查表或视图名称是否写错:3.检查Java中数据源的数据库用户是否具有引用该表或视图的权限:
- Uploadify/uploadifive上传(中文文档)
Uploadify是一款基于JQuery的优秀的文件/图片上传的插件,有基于Flash和HTML5两种版本. Uploadify/uploadifive主要特点有: 1. 多文件上传 2. 个性化设 ...
- python之在线PK游戏(第六天)
本节作业: 熟练使用类和模块,写一个交互性强.有冲突的程序. 故本次写了一个文字回合制的PK游戏,系统主程序为根目录下的:game_menu.py 1. 系统功能模块: 第六天的作业:文字游戏程序 ...
- C#语法糖,让编程更具乐趣
一.什么是语法糖 语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法 ...
- Linux中的命令 make -f 是什么意思
出处:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422460 ...
- linux的whoami, who指令
who 这个命令重点在用来查看当前有那些用户登录到了本台机器上 whoami 显示的是有效用户用户名 ,是当前操作用户的用户名 who am i == who -m 显示的是实际用户的用户名,即用户登 ...