Python学习笔记——进阶篇【第九周】———MYSQL操作
Mysql 增删改查操作
查看数据库
show databases; 创建数据库并允许中文插入
create database s12day9 charset utf8; 使用数据库
use s12day9; 查看表
show tables; 创建表
create table students
(
id int not null auto_increment primary key,
name char(32) not null,
sex char(20) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
); 查看表结构
desc students; 查看创建sql的语句(查看别人创建的sql语句,InnoDB 支持事物操作)
show create table students; 插入数据
insert into students(name,sex,age,tel) values('alex','man',18,'') 删除数据
delete from students where id =2; 修改数据
update students set name = 'sb' where id =1; 查询数据
select * from students 查询年龄大于20学生的所有数据
select * from students where age> 20; 查询年龄大于20男生的所有数据
select * from students where age> 20 and sex='man'; 查询年龄中含1学生的所有数据(模糊查询,%代表所有)
selsct * from students where age like "1%"; 查询年龄中含1学生的姓名和性别(模糊查询,%代表所有)
select name,sex from students where age like "1%"; 修改数据
update students set age =26 where name ='alex'; 查询数据
select * from students 批量修改
update students set age=26; 删除数据
delete from students where name='rain'; 插入字段
alter table students add colum nal char(64); 查看表结构
desc students;
MYSQL增删改查操作
python MySQL API
import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
cur = conn.cursor()
reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa'))
# reCount = cur.execute('insert into UserInfo(Name,Address) values(%(id)s, %(name)s)',{'id':12345,'name':'wupeiqi'})
conn.commit()
cur.close()
conn.close()
print reCount
插入数据
import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb') cur = conn.cursor() li =[
('alex','usa'),
('sb','usa'),
]
reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li) conn.commit()
cur.close()
conn.close() print reCount
批量插入数据
import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
cur = conn.cursor()
reCount = cur.execute('delete from UserInfo')
conn.commit()
cur.close()
conn.close()
print reCount
删除数据
import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
cur = conn.cursor()
reCount = cur.execute('update UserInfo set Name = %s',('alin',))
conn.commit()
cur.close()
conn.close()
print reCount
修改数据
# ##################### fetchone 取一条数据/fetchmany(num) 指定取几条数据##################### import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
cur = conn.cursor() reCount = cur.execute('select * from UserInfo') print cur.fetchone()
print cur.fetchone()
cur.scroll(-1,mode='relative')
print cur.fetchone()
print cur.fetchone()
cur.scroll(0,mode='absolute')
print cur.fetchone()
print cur.fetchone() cur.close()
conn.close() print reCount # ###################### fetchall 取所有数据############################## import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
cur = conn.cursor() reCount = cur.execute('select Name,Address from UserInfo') nRet = cur.fetchall() cur.close()
conn.close() print reCount
print nRet
for i in nRet:
print i[0],i[1]
查数据
事物的回滚(插入2条数据——回滚——提交)
import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='mydb')
cur = conn.cursor()
reCount = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)',('Jack','F',22,12342345,'name',"CN"))
reCount = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)',('Rachel','F',26,35642345,'name',"CN"))
conn.rollback()
conn.commit() cur.close()
conn.close() print reCount
事物的回滚
SQL的详细讲解:http://www.cnblogs.com/wupeiqi/articles/5095821.html
SQL的基本使用:http://www.cnblogs.com/Eva-J/p/5133716.html
Python学习笔记——进阶篇【第九周】———MYSQL操作的更多相关文章
- Python学习笔记进阶篇——总览
Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(Socket编程进阶&多线程.多进程) Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(异常处理) Pyth ...
- Python学习笔记——进阶篇【第九周】———线程、进程、协程篇(队列Queue和生产者消费者模型)
Python之路,进程.线程.协程篇 本节内容 进程.与线程区别 cpu运行原理 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Ev ...
- Python学习笔记——进阶篇【第九周】———协程
协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...
- Python学习笔记——进阶篇【第八周】———进程、线程、协程篇(Socket编程进阶&多线程、多进程)
本节内容: 异常处理 Socket语法及相关 SocketServer实现多并发 进程.线程介绍 threading实例 线程锁.GIL.Event.信号量 生产者消费者模型 红绿灯.吃包子实例 mu ...
- Python学习笔记基础篇——总览
Python初识与简介[开篇] Python学习笔记——基础篇[第一周]——变量与赋值.用户交互.条件判断.循环控制.数据类型.文本操作 Python学习笔记——基础篇[第二周]——解释器.字符串.列 ...
- PHP学习笔记 - 进阶篇(11)
PHP学习笔记 - 进阶篇(11) 数据库操作 PHP支持哪些数据库 PHP通过安装相应的扩展来实现数据库操作,现代应用程序的设计离不开数据库的应用,当前主流的数据库有MsSQL,MySQL,Syba ...
- PHP学习笔记 - 进阶篇(8)
PHP学习笔记 - 进阶篇(8) 日期与时间 取得当前的Unix时间戳 UNIX 时间戳(英文叫做:timestamp)是 PHP 中关于时间与日期的一个很重要的概念,它表示从 1970年1月1日 0 ...
- PHP学习笔记 - 进阶篇(2)
PHP学习笔记 - 进阶篇(2) 函数 1.自定义函数 PHP内置了超过1000个函数,因此函数使得PHP成为一门非常强大的语言.大多数时候我们使用系统的内置函数就可以满足需求,但是自定义函数通过将一 ...
- PHP学习笔记 - 进阶篇(10)
PHP学习笔记 - 进阶篇(10) 异常处理 抛出一个异常 从PHP5开始,PHP支持异常处理,异常处理是面向对象一个重要特性,PHP代码中的异常通过throw抛出,异常抛出之后,后面的代码将不会再被 ...
随机推荐
- CSS盒模型和定位的类型
此文根据Steven Bradley的<How Well Do You Understand CSS Positioning?>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之处 ...
- HTTP method GET is not supported by this URL(转)
源地址:http://blog.csdn.net/qfs_v/article/details/2545168 Servlet eroor:HTTP method GET is not support ...
- IOS学习之路(代码实现自动布局)
1.将一个试图放置在其父视图的中央位置,使用限制条件. 2.创建两个限制条件:一个是将目标视图的 center.x 位置排列在其父视图的 center.x 位置,并且另外一个是将目标视图的 cente ...
- c语言,递归翻转一个单链表,c实现单链表
目的:主要是练习c里面单链表的实现,递归思想复习; #include <stdlib.h> #include <stdio.h> typedef struct _Node{// ...
- 生成GUID字符串
//生成GUID字符串 string loginToken = System.Guid.NewGuid().ToString();
- SQL Server 2008 - Cannot set a credential for principal 'sa'.
SQL Server 2008 - Cannot set a credential for principal 'sa'. 很久没有用到SQL Server了,今天有幸在帮同事解决一个SQL Serv ...
- DNS:域名系统
DNS:域名系统 1.DNS DNS 是计算机域名系统(Domain Name System 或Domain Name Service) 的缩写,它是由解析器以及域名服务器组成的.域名服务器 ...
- ASP.NET MVC基础学习
ASP.NET MVC基础学习 传统的MVC概念 模型:组类,描述了要处理的数据以及修改和操作数据的业务规则 视图:定义应用程序用户界面的显示方式 控制器:一组类,用来处理来自用户,整个应用程序流以及 ...
- map 类型
map 是键-值对的集合.map 类型通常可理解为关联数组(associative array): 可使用键作为下标来获取一个值,正如内置数组类型一样.而关联的本质在于元素的值与某个特定的键相关联,而 ...
- iOS之Xcode 8.0真机调试运行:This ** is running iOS 10.1.1 (14B100), which may not be supported
2016年10月份 苹果升级了iOS系统为10.1,xcode 8.0 运行会提示: This iPhone 5 (Model A1429) is running iOS 10.1.1 (14B100 ...