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操作的更多相关文章

  1. Python学习笔记进阶篇——总览

    Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(Socket编程进阶&多线程.多进程) Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(异常处理) Pyth ...

  2. Python学习笔记——进阶篇【第九周】———线程、进程、协程篇(队列Queue和生产者消费者模型)

    Python之路,进程.线程.协程篇 本节内容 进程.与线程区别 cpu运行原理 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Ev ...

  3. Python学习笔记——进阶篇【第九周】———协程

    协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...

  4. Python学习笔记——进阶篇【第八周】———进程、线程、协程篇(Socket编程进阶&多线程、多进程)

    本节内容: 异常处理 Socket语法及相关 SocketServer实现多并发 进程.线程介绍 threading实例 线程锁.GIL.Event.信号量 生产者消费者模型 红绿灯.吃包子实例 mu ...

  5. Python学习笔记基础篇——总览

    Python初识与简介[开篇] Python学习笔记——基础篇[第一周]——变量与赋值.用户交互.条件判断.循环控制.数据类型.文本操作 Python学习笔记——基础篇[第二周]——解释器.字符串.列 ...

  6. PHP学习笔记 - 进阶篇(11)

    PHP学习笔记 - 进阶篇(11) 数据库操作 PHP支持哪些数据库 PHP通过安装相应的扩展来实现数据库操作,现代应用程序的设计离不开数据库的应用,当前主流的数据库有MsSQL,MySQL,Syba ...

  7. PHP学习笔记 - 进阶篇(8)

    PHP学习笔记 - 进阶篇(8) 日期与时间 取得当前的Unix时间戳 UNIX 时间戳(英文叫做:timestamp)是 PHP 中关于时间与日期的一个很重要的概念,它表示从 1970年1月1日 0 ...

  8. PHP学习笔记 - 进阶篇(2)

    PHP学习笔记 - 进阶篇(2) 函数 1.自定义函数 PHP内置了超过1000个函数,因此函数使得PHP成为一门非常强大的语言.大多数时候我们使用系统的内置函数就可以满足需求,但是自定义函数通过将一 ...

  9. PHP学习笔记 - 进阶篇(10)

    PHP学习笔记 - 进阶篇(10) 异常处理 抛出一个异常 从PHP5开始,PHP支持异常处理,异常处理是面向对象一个重要特性,PHP代码中的异常通过throw抛出,异常抛出之后,后面的代码将不会再被 ...

随机推荐

  1. [原]逆向iOS SDK -- +[UIImage imageNamed:] 的实现

    汇编代码: ; Dump of assembler code for function +[UIImage imageNamed:] ; R0 = UIImage, R1 = "imageN ...

  2. 记录最近在使用sprintf构造字符串时遇到的一个问题

    直接上代码: #include "stdio.h" #include "string.h" int main() { char szTmp[20] = {0}; ...

  3. kindeditor使用方法

    一.下载编辑器 下载KindEditor最新版本(本版本为4.1.10) 下载页面:http://kindeditor.net/down.php 二.部署编辑器 解压kindeditor-x.x.x. ...

  4. JS简单验证密码强度

    <input type="password" id="password" value=""/><button id=&qu ...

  5. jQuery中bind与live的用法与区别

    首先介绍这两个方法之前,我们常用的是click()方法 $("a").click(function() { alert("hello"); }); click( ...

  6. wxWidgets的安装编译、相关配置、问题分析处理

    wxWidgets的安装编译.相关配置.问题分析处理 一.介绍部分 (win7 下的 GUI 效果图见 本篇文章的最后部分截图2张) wxWidgets是一个开源的跨平台的C++构架库(framewo ...

  7. windows下配置PHP+Nginx+MySQL完整流程(转)

    对于在windows上的php+nginx的配置可能好多同学一次根本都配不正确,于我也是如此,为此我将我成功配置的过程细致的总结如下,希望能帮助搞PHP研究的同学 1.资源准备 MySQL:这个链接不 ...

  8. c++中的类型擦除

    (原创)c++中的类型擦除 c++11 boost技术交流群:296561497,欢迎大家来交流技术. 关于类型擦除,可能很多人都不清楚,不知道类型擦除是干啥的,为什么需要类型擦除.有必要做个说明,类 ...

  9. NCache:最新发布的.NET平台分布式缓存系统

    NCache:最新发布的.NET平台分布式缓存系统在等待Microsoft完成Velocity这个.NET平台下的分布式内存缓存系统的过程中,现在让我们将目光暂时投向其他已经有所建树的软件开发商.Al ...

  10. 横瓜从WP7与WP8不兼容之处预测微软公司在未来20年将会倒闭

    横瓜(601069289)  12:40:50 微软频繁升级操作系统版本捞钱,还不向下兼容,其企业的核心竞争力不再是技术,是钱!除了操作系统,其它产品没有任何核心技术和竞争力,微软必死. 横瓜从WP7 ...