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模块的更多相关文章

  1. Python操作mysql之模块pymysql

    pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文环境 python3.6.1  Mysql ...

  2. python开发学习-day09(队列、多路IO阻塞、堡垒机模块、mysql操作模块)

    s12-20160312-day09 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  3. Python安装MySQL数据库模块

    背景 折腾: [记录]使用Python操作MySQL数据库 的过程中,需要去安装MySQLdb. 下载MySQLdb 去官网: http://pypi.python.org/pypi/MySQL-py ...

  4. Python基础篇【第5篇】: Python模块基础(一)

    模块 简介 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就 ...

  5. python模块使用案例

    python模块使用案例 一.使用MySQLdb模块代码示例: # 导入 MySQLdb模块 import MySQLdb # 和服务器建立链接,host是服务器ip,我的MySQL数据库搭建在本机, ...

  6. python 模块 SQLalchemy

    SQLalchemy 概述: # &&&&&&&&&&&&&&&&&am ...

  7. python 模块和包

    一,模块 1,什么是模块? 常见的场景: 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py 的后缀. 但其实 import 加载的模块分为四个通用类别: 1,使用pyt ...

  8. Python模块和包使用

    1.什么是模块 模块就是一个.py的文件 2.为什么要使用模块? 最开始的程序(没有任何组织)----> 函数------>类----->模块------>包  为了让程序的组 ...

  9. python模块大全

    python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...

  10. [转]Python 模块收集

    Python 模块收集 转自:http://kuanghy.github.io/2017/04/04/python-modules Python | Apr 4, 2017 | python 工具 a ...

随机推荐

  1. Python开发入门与实战1-开发环境

    1.搭建Python Django开发环境 1.1.Python运行环境安装 Python官网:http://www.python.org/ Python最新源码,二进制文档,新闻资讯等可以在Pyth ...

  2. 一次蜿蜒曲折的RFID破解之路

    前言 早一段时间看到一篇看雪论坛关于逻辑嗅探破解接触式IC卡口令的文章,激起鄙人对rfid的兴趣.遂准备拿学校的卡一展身手. 0×00 前期准备 经过初步了解,学校的rfid卡片分为两种.校园卡采用M ...

  3. HackRF实现GPS欺骗教程

    硬件平台:HackRF One软件平台:MAC运行环境搭建系统平台:OS X 10.11 EI CapitanGPS终端:One Plus手机,飞行模式,仅GPS定位,GPS test App文章特点 ...

  4. Java Proxy

    Client---->Interface A --        -- 代理类     Class AImpl 代理类是动态生成的,借助Proxy类和InvocationHandler接口进行实 ...

  5. 【Oracle XE系列之三】使用OMF方式手工创建Oracle XE数据库

    环境:win10_X64_Pro 1.创建数据库实例,实例名为PF C:\oraclexe\app\oracle\product\11.2.0\server\bin>oradim -new -s ...

  6. 1、SQL基础整理(基本查询)

    分离.附加.备份.还原 --去重 select distinct 列名from表名 --更新 update fenshu set name = ‘李四’where code = 9 --更改表名 sp ...

  7. WP8.1 双击两次返回键退出程序

    在实现Windows Phone上实现点按两次返回键退出程序, 一种方法是使用Coding4Fun提供的ToastPrompt, 使用方法如下: 1. 安装引用, 打开Package Manager ...

  8. ASP.NET中的指令:

    来源:http://www.cnblogs.com/zhuisha/archive/2008/07/02/1234222.html ASP.NET中的指令: @Page指令: @Page指令只能在.a ...

  9. java 基本类型

    1 常量 整数 byte 1字节 8位 -27~27-1 0111 1111 ~1000 0000 short 2 16     int 4 32     long 8 64     1111 111 ...

  10. CSS 实现:两栏布局(一边固定,一边自适应)

    ☊[实现要求]:CSS实现左边固定,右边自适应父容器宽度的两栏布局. <body> <div class="left"></div> <d ...