MySQL Python教程(3)
Class cursor.MySQLCursor
具体方法和属性如下:
Constructor cursor.MySQLCursor
Method MySQLCursor.callproc(procname, args=())
Method MySQLCursor.close()
Method MySQLCursor.execute(operation, params=None, multi=False)
Method MySQLCursor.executemany(operation, seq_params)
Method MySQLCursor.fetchall()
Method MySQLCursor.fetchmany(size=1)
Method MySQLCursor.fetchone()
Method MySQLCursor.fetchwarnings()
Method MySQLCursor.stored_results()
Property MySQLCursor.column_names
Property MySQLCursor.description
Property MySQLCursor.lastrowid
Property MySQLCursor.statement
Property MySQLCursor.with_rows
Constructor cursor.MySQLCursor
使用MySQLConnection对象来初始化
Method MySQLCursor.callproc(procname, args=())
调用procname程序,args要包含所有需要用到的参数。返回值类型为MySQLCursorBuffered。
# Definition of the multiply stored procedure:
# CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
# BEGIN
# SET pProd := pFac1 * pFac2;
# END
>>> args = (5, 5, 0) # 0 is to hold value of the OUT parameter pProd
>>> cursor.callproc('multiply', args)
('5', '5', 25L)
Method MySQLCursor.close()
每次使用完cursor后,调用该函数关闭。
Method MySQLCursor.execute(operation, params=None, multi=False)
该函数用来提出针对数据库的操作。params是操作中的参数。
insert = (
"INSERT INTO employees (emp_no, first_name, last_name, hire_date) "
"VALUES (%s, %s, %s, %s)")
data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
cursor.execute(insert, data)
select = "SELECT * FROM employees WHERE emp_no = %(emp_no)s"
cursor.execute(select, { 'emp_no': 2 })
如果multi参数设置为true,则可以执行多条语句,返回值为指向每个结果的迭代器。
operation = 'SELECT 1; INSERT INTO t1 VALUES (); SELECT 2'
for result in cursor.execute(operation):
if result.with_rows:
print("Statement '{}' has following rows:".format(
result.statement))
print(result.fetchall())
else:
print("Affected row(s) by query '{}' was {}".format(
result.statement, result.rowcount))
Method MySQLCursor.executemany(operation, seq_params)
数据库操作operation会执行多次,直至seq_params中所有参数执行完毕。
data = [
('Jane', date(2005, 2, 12)),
('Joe', date(2006, 5, 23)),
('John', date(2010, 10, 3)),
]
stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
cursor.executemany(stmt, data)
Method MySQLCursor.fetchall()
返回查询的结果集合。
>>> cursor.execute("SELECT * FROM employees ORDER BY emp_no")
>>> head_rows = cursor.fetchmany(size=2)
>>> remaining_rows = cursor.fetchall()
Method MySQLCursor.fetchmany(size=1)
返回接下来的size个查询结果,如果没有足够的结果,则返回空的list。
Method MySQLCursor.fetchone()
返回接下来的一个查询结果,该函数在fetchamany()和fetchalll()中调用。
# Using a while-loop
cursor.execute("SELECT * FROM employees")
row = cursor.fetchone()
while row is not None:
print(row)
row = cursor.fetchone()
# Using the cursor as iterator
cursor.execute("SELECT * FROM employees")
for row in cursor:
print(row)
Method MySQLCursor.fetchwarnings()
设置get_warnings为true后,能通过该函数获取警告元组。
>>> cnx.get_warnings = True
>>> cursor.execute('SELECT "a"+1')
>>> cursor.fetchall()
[(1.0,)]
>>> cursor.fetchwarnings()
[(u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'a'")]
Method MySQLCursor.stored_results()
调用 callproc()后,产生的结果集合可以用该函数获取。
>>> cursor.callproc('sp1')
()
>>> for result in cursor.stored_results():
... print result.fetchall()
...
[(1,)]
[(2,)]
Property MySQLCursor.column_names
只读属性。返回一个Unicode编码的string,为结果集合的列名称。
cursor.execute("SELECT last_name, first_name, hire_date "
"FROM employees WHERE emp_no = %s", (123,))
row = dict(zip(cursor.column_names, cursor.fetchone())
print("{last_name}, {first_name}: {hire_date}".format(row))
Property MySQLCursor.description
返回cursor结果集合的描述。
import mysql.connector
from mysql.connector import FieldType
...
cursor.execute("SELECT emp_no, last_name, hire_date "
"FROM employees WHERE emp_no = %s", (123,))
for i in range(len(cursor.description)):
print("Column {}:".format(i+1))
desc = cursor.description[i]
print("column_name = {}".format(desc[0]))
print("type = {} ({})".format(desc[1], FieldType.get_info(desc[1])))
print("null_ok = {}".format(desc[6]))
print("column_flags = {}".format(desc[7]))
输出如下:
Column 1:
column_name = emp_no
type = 3 (LONG)
null_ok = 0
column_flags = 20483
Column 2:
column_name = last_name
type = 253 (VAR_STRING)
null_ok = 0
column_flags = 4097
Column 3:
column_name = hire_date
type = 10 (DATE)
null_ok = 0
column_flags = 4225
Property MySQLCursor.lastrowid
返回最近修改的列的id值。
Property MySQLCursor.statement
返回上一次的执行结果。
Property MySQLCursor.with_rows
如果返回结果提供rows,该值为true。
MySQL Python教程(3)的更多相关文章
- MySQL Python教程(1)
首先对于数据库的基本操作要有一定基础,其次了解Python的基础语法. 建议先阅读MysqL中文教程http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chap ...
- MySQL Python教程(2)
mysql官网关于python的API是最经典的学习材料,相信对于所有函数浏览一遍以后,Mysql数据库用起来一定得心应手. 首先看一下Connector/Python API包含哪些类和模块. Mo ...
- MySQL Python教程(4)
Class cursor.MySQLCursorBuffered 该类从Class cursor.MySQLCursorBuffered继承,如果需要,可以在执行完SQL语句后自动缓冲结果集合.imp ...
- Python教程:操作数据库,MySql的安装详解
各位志同道合的同仁请点击上方关注 本教程是基于Python语言的深入学习.本次主要介绍MySql数据库软件的安装.不限制语言语法,对MySql数据库安装有疑惑的各位同仁都可以查看一下. 如想查看学习P ...
- 数据库 之MySQL 简单教程
So Easy系列之MySQL数据库教程 1. 数据库概述 1.1. 数据库概述 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和 ...
- Python教程:连接数据库,对数据进行增删改查操作
各位志同道合的同仁可以点击上方关注↑↑↑↑↑↑ 本教程致力于程序员快速掌握Python语言编程. 本文章内容是基于上次课程Python教程:操作数据库,MySql的安装详解 和python基础知识之上 ...
- Windows下安装MySQL详细教程
Windows下安装MySQL详细教程 1.安装包下载 2.安装教程 (1)配置环境变量 (2)生成data文件 (3)安装MySQL (4)启动服务 (5)登录MySQL (6)查询用户密码 (7 ...
- MySQL 【教程二】
MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: # CREATE TABLE table_name (c ...
- python入门灵魂5问--python学习路线,python教程,python学哪些,python怎么学,python学到什么程度
一.python入门简介 对于刚接触python编程或者想学习python自动化的人来说,基本都会有以下python入门灵魂5问--python学习路线,python教程,python学哪些,pyth ...
随机推荐
- React Native 开发之 (07) 常用组件-View
掌握了React Native的组件就可以使用IOS的原生组件和API. 一 View组件 就像开发web应用程序中,需要使用很多的HTML标签.例如 div,form.但是在基于DIV+CSS布局的 ...
- 01C语言基础知识
C语言知识要点总结 1在C语言中,关系运算的结果为“真”,就返回1,"假"就返回0 printf("input value is %d" , 5 > 4) ...
- BZOJ2458: [BeiJing2011]最小三角形
类似分治最近点对的方法乱搞一下就行. #include<bits/stdc++.h> #define N 200010 #define M (s+t>>1) using nam ...
- JavaScript学习笔记——节点
javascript-节点属性详解 根据 DOM,HTML 文档中的每个成分都是一个节点. DOM 是这样规定的: 整个文档是一个文档节点 每个 HTML 标签是一个元素节点 包含在 HTML 元素中 ...
- ecshop程序结构说明
文件夹说明 名称 备注(作用或意义) 根目录 前台程序文件 admin 后台程序文件 admin/help 功能的帮助文件 admin/images 后台页面用图片 admin/includes 后台 ...
- thinkphp连接数据库
版本:3.1.1 连接数据库的具体位置 thinkphp/Config/convention.php,默认修改数据库在这里就可以了 但是为了方便,把数据库配置写到Index/Conf/config.p ...
- 设计模式学习——策略模式(Strategy Pattern)
0. 前言 最近在重构公司的一个项目的时候,在抽取DES加密重复部分代码的时候,突然间想起了策略模式,感觉策略模式好像可以应用上,于是重新学习了下策略模式.注:在DES加密中,有DES和TDES算法, ...
- tomcat 无法加载js和css 等静态文件的问题
前段时间做了个网站,在本地tomcat测试都没有问题,但是部署到阿里云上之后,系统样式全没了.jsp等动态页面访问正常. 打开浏览器监控发现所有的css 和js 文件返回都是404 .直接访问单个的c ...
- MSSQL数据库索引的应用
一.索引的概念 索引就是加快检索表中数据的方法.数据库的索引类似于书籍的索引.在书籍中,索引允许用户不必翻阅完整个书就能迅速地找到所需要的信息.在数据库中,索引也允许数据库程序迅速地找到表中的数据,而 ...
- BloomFilter 与 Cuckoo Filter
BloomFilter 与 CuckooFilter Bloom Filter 原理 Bloom Filter是一种空间效率很高的随机数据结构,它的原理是,当一个元素被加入集合时,通过K个相互独立的H ...