MySQL-python模块
1. Python 操作 Mysql 模块的安装
linux:
pip install MySQL-python 或 yum -y install MySQL-python
windows: exe安装包
http://pan.baidu.com/s/1c2ugfvE
2. SQL基本使用
2.1 数据库操作
show databases;
use [databasename];
create database [databasename] charset utf8;
2.2 数据表操作
create database py CHARSET('utf8')
use py
create table students (
id int not null auto_increment primary key,
name char(32) not null,
sex char(12) not null,
age tinyint unsigned not null,
tel char(13) null default "-",
nal char(64)
);
2.3 数据操作
insert into students(name,sex,age,tel,nal) values('alex','man',18,'','CN')
delete from students where id =2;
update students set name = 'sb' where id =1;
select * from students
2.4 其他
主键
外键
左右连接3. Python MySQL API
3.1 插入数据
#!/use/bin/env python
# -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect('py', user='root', passwd='py123', db='py')
cur = conn.cursor() info = [ ('go1','man',30,'13900000000', 'US'), ('go2','man',28,'13900000001', 'HK'),] info1 = ('mihui','man',30,'', 'US')
info2 = ['YeGangchan','man',28,'', 'HK']
#reCount = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)', info1)
reCount = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)', info2)
reCount2 = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)', ('rain','female',18,'','NK'))
# 插入多条
reCount3 = cur.executemany('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)', info)
conn.commit()
cur.close()
conn.close()
print(reCount)
注意:cur.lastrowid
3.2 删除数据
#!/use/bin/env python
# -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='py', user='root', passwd='py123', db='py')
cur = conn.cursor() reCount = cur.execute('delete from students where name=%s',('syl')) conn.commit()
cur.close()
conn.close() print(reCount)
3.3 修改数据
#!/use/bin/env python
# -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='py', user='root', passwd='py123', db='py') cur = conn.cursor()
reCount = cur.execute('update students set name=%s where name=%s',('Laonanhai', 'Oldboy'))
conn.commit()
cur.close()
conn.close()
print(reCount)
3.4 查询数据
#!/use/bin/env python
# -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect('py', user='root', passwd='py123', db='py') cur = conn.cursor() reCount = cur.execute('select * from students')
## fetchone()
print(cur.fetchone())
print(cur.fetchone())
print(cur.fetchone())
cur.scroll(-1, mode='relative') #游标相对上移一个
print(cur.fetchone())
print(cur.fetchone()) print('##------')
cur.scroll(0, mode='absolute') #游标移动到绝对位置0
# print(cur.fetchone())
# print(cur.fetchone()) print('##------ fetchmany()')
# print(cur.fetchmany())
print(cur.fetchmany(4)) cur.close()
conn.close() print(reCount)
#!/use/bin/env python
# -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='py', user='root', passwd='py123', db='py')
cur = conn.cursor()
#cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) reCount = cur.execute('select * from students') nRes = cur.fetchall()
cur.close()
conn.close() print(reCount)
print(nRes)
for i in nRes:
print(i[0],i[1])
3.5 事务回滚
students表创建语句:
CREATE TABLE `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(32) NOT NULL,
`sex` char(12) DEFAULT NULL,
`age` tinyint(3) unsigned NOT NULL,
`tel` char(13) DEFAULT '_',
`nal` char(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
如:select * from students现在的结果如下(id自增)

现在的最后一条数据的id为6
插入两条数据在commit()前行rollback()
#!/use/bin/env python
# -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='py', user='root', passwd='py123', db='py')
cur = conn.cursor() reCount = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)', ('rain2','man',26,'','NK'))
reCount2 = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)', ('rain3','female',22,'','NK')) conn.rollback()
conn.commit()
cur.close()
conn.close()
print(reCount)
print(reCount2)
现在再来
select * from students现在的结果如下

最后一条数据的仍id为6
做一次真正数据插入:
#!/use/bin/env python
# -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect(host='py', user='root', passwd='py123', db='py')
cur = conn.cursor() reCount = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)', ('rain2','man',26,'','NK'))
reCount2 = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)', ('rain3','female',22,'','NK')) #conn.rollback()
conn.commit()
cur.close()
conn.close()
print(reCount)
print(reCount2)
select * from students现在的结果如下

结果id跳过了7,8
id AUTO_INCREMENT信息保存在内存中,rollback时不回滚AUTO_INCREMENT信息
MySQL-python模块的更多相关文章
- Python操作mysql之模块pymysql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文环境 python3.6.1 Mysql ...
- python开发学习-day09(队列、多路IO阻塞、堡垒机模块、mysql操作模块)
s12-20160312-day09 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- Python安装MySQL数据库模块
背景 折腾: [记录]使用Python操作MySQL数据库 的过程中,需要去安装MySQLdb. 下载MySQLdb 去官网: http://pypi.python.org/pypi/MySQL-py ...
- Python基础篇【第5篇】: Python模块基础(一)
模块 简介 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就 ...
- python模块使用案例
python模块使用案例 一.使用MySQLdb模块代码示例: # 导入 MySQLdb模块 import MySQLdb # 和服务器建立链接,host是服务器ip,我的MySQL数据库搭建在本机, ...
- python 模块 SQLalchemy
SQLalchemy 概述: # &&&&&&&&&&&&&&&&&am ...
- python 模块和包
一,模块 1,什么是模块? 常见的场景: 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py 的后缀. 但其实 import 加载的模块分为四个通用类别: 1,使用pyt ...
- Python模块和包使用
1.什么是模块 模块就是一个.py的文件 2.为什么要使用模块? 最开始的程序(没有任何组织)----> 函数------>类----->模块------>包 为了让程序的组 ...
- python模块大全
python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...
- [转]Python 模块收集
Python 模块收集 转自:http://kuanghy.github.io/2017/04/04/python-modules Python | Apr 4, 2017 | python 工具 a ...
随机推荐
- SpringMvc中的反射
controller中的方法,是通过反射调用的 spring监控controller中的注解,当命令符合某个注解的时候,通过反射,找到这个注解对应的方法,然后调用,处理完成得到返回值,再根据这个返回值 ...
- JSP专题
JSP起源 ·在很多动态网页中,绝大部分内容都是固定不变的,只有局部内容需要动态产生和改变. ·如果使用Servlet程序来输出只有局部内容需要动态改变的网页,其中所有的静态内容也需要程序员代码产生, ...
- 国内android帮助文档镜像网站---http://wear.techbrood.com/develop/index.html
http://wear.techbrood.com/develop/index.html
- Javascript ----字符串(String)中的方法
涉及字符串时,常用到的几个方法... --------------------------------------------------------------------------------- ...
- Cookies和Session的区别
原文:http://www.cnblogs.com/lijihong/p/4743818.html 今天主要学习了Cookies和Session,网络上关于这方面的知识可谓很多,让人眼花缭乱,在此作一 ...
- BZOJ 3999 旅游
.......好长啊. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- 怎样修改Response中的内容
重写Stream public class CatchTextStream : Stream { private Stream output; public CatchTextStream(Strea ...
- Mac环境下装node.js,npm,express
1. 下载node.js for Mac 地址: http://nodejs.org/ 直接下载 pkg的,双击安装,一路点next,很容易就搞定了. 安装完会提醒注意 node和npm的路径是 /u ...
- 12-1 上午mysql 基本语句
create table test( code varchar(20) primary key, name varchar(20)); 关键字primary key 主键非空 not nullfore ...
- XML HttpRequest
XMLHttpRequest对象提供了在网页加载后与服务器进行通信的方法. 使用XMLHttpRequest对象,能够: 在不重新加载页面的情况下更新网页 在页面已加载后从服务器请求数据,接受数据 在 ...