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 ...
随机推荐
- css名词解释
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sql查询指定表外键约束
//////////////////查询指定表外键约束select a.name as 约束名, object_name(b.parent_object_id) as 外键表, d.name as 外 ...
- Find linux下
find 1.作用 find命令的作用是在目录中搜索文件,它的使用权限是所有用户. 2.格式 find [path][options][expression] path指定目录路径,系统从这里开始沿着 ...
- tomcat 开机后台运行
引用:http://jingyan.baidu.com/article/a65957f4b12b8724e77f9b5a.html Tomcat是Apache 软件基金会(Apache Softwar ...
- Extjs swfUpload 多图片上传
一.属性介绍 类型 默认值 描述 upload_url String 处理上传文件的服务器端页面的url地址,可以是绝对地址,也可以是相对地址,当为相对地址时相对的是当前代码所在的文档地址 p ...
- Sql server 2008镜像配置步骤
下面主要是2008 MSSQL的一个镜像安装步骤,使用到的工具是mssql自带的Management Studio purpose : SQL SERVER 2008 mirror configura ...
- YbSoftwareFactory 代码生成插件【二十二】:CMS基础功能的实现
很多网友建议在YbRapidSolution for MVC框架的基础上实现CMS功能,以方便进行内容的管理,加快前端页面的开发速度.因此花了一段时间,实现了一套CMS内容发布系统并已集成至YbRap ...
- ClickOnce添加自定义prerequisite
参考http://www.codeproject.com/Articles/15863/Add-your-own-custom-prerequisite-to-quot-ClickOnce 下载地址: ...
- ClientScript.RegisterStartupScript 不起作用
asp.net webform 使用 ClientScript.RegisterStartupScript 不起作用 form 加上 runat="server",ok
- Linux下面配置文件~/.bash_profile
~/.的意义是什么? ~ 代表你的/home/用户名目录 假设你的用户名是x,那么~/就是/home/x/ . 是代表此目录本身,但是一般可以不写 所以cd ~/. 和cd ~ 和cd ~/效果是一样 ...