MySQL Python教程(4)
2014-03-14 21:32
281 查看
Class cursor.MySQLCursorBuffered
该类从Class cursor.MySQLCursorBuffered继承,如果需要,可以在执行完SQL语句后自动缓冲结果集合。
import mysql.connector
cnx = mysql.connector.connect()
# Only this particular cursor will buffer results
cnx.cursor(buffered=True)
# All cursors will be buffering by default
cnx2 = mysql.connector.connect(buffered=True)
Class cursor.MySQLCursorPrepared
该类继承cursor.MySQLCursor,使用二进制协议执行prepare statement
使用方法:
import mysql.connector
from mysql.connector.cursor import MySQLCursorPrepared
cnx = mysql.connector.connect(database='employees')
cursor = cnx.cursor(cursor_class=MySQLCursorPrepared)
此时cursor为MySQLCursorPrepared对象。
举例:
cursor = cnx.cursor(cursor_class=MySQLCursorPrepared)
stmt = "SELECT fullname FROM employees WHERE id = ?" # (1)
cursor.execute(stmt, (5,)) # (2)
# ... fetch data ...
cursor.execute(stmt, (10,)) # (3)
# ... fetch data ...
Class constants.ClientFlag
This class provides constants defining MySQL client flags that can be used when the connection is established to configure the session.
>>> import mysql.connector
>>> mysql.connector.ClientFlag.FOUND_ROWS
2
Class constants.FieldType
该类不能被实例化,支持所有MySQL的数据类型。
from __future__ import print_function
import mysql.connector
from mysql.connector import FieldType
cnx = mysql.connector.connect(user='scott', database='test')
cursor = cnx.cursor()
cursor.execute(
"SELECT DATE(NOW()) AS `c1`, TIME(NOW()) AS `c2`, "
"NOW() AS `c3`, 'a string' AS `c4`, 42 AS `c5`")
rows = cursor.fetchall()
for desc in cursor.description:
colname = desc[0]
coltype = desc[1]
print("Column {} has type {}".format(
colname, FieldType.get_info(coltype)))
cursor.close()
cnx.close()
Class constants.SQLMode
提供所有已知的SQL服务器模式。具体参见 http://dev.mysql.com/doc/refman/5.6/en/server-sql-mode.html
Class constants.CharacterSet
提供MYSQL的字符集和默认的collations。参见Method MySQLConnection.set_charset_collation
Class constants.RefreshOption
该类提供多种flush的操作。
RefreshOption.GRANT
Refresh the grant tables, like FLUSH PRIVILEGES.
RefreshOption.LOG
Flush the logs, like FLUSH LOGS.
RefreshOption.TABLES
Flush the table cache, like FLUSH TABLES.
RefreshOption.HOSTS
Flush the host cache, like FLUSH HOSTS.
RefreshOption.STATUS
Reset status variables, like FLUSH STATUS.
RefreshOption.THREADS
Flush the thread cache.
RefreshOption.SLAVE
On a slave replication server, reset the master server information and restart the slave, like RESET SLAVE.
RefreshOption.MASTER
On a master replication server, remove the binary log files listed in the binary log index and truncate the index file, like RESET MASTER.
该类从Class cursor.MySQLCursorBuffered继承,如果需要,可以在执行完SQL语句后自动缓冲结果集合。
import mysql.connector
cnx = mysql.connector.connect()
# Only this particular cursor will buffer results
cnx.cursor(buffered=True)
# All cursors will be buffering by default
cnx2 = mysql.connector.connect(buffered=True)
Class cursor.MySQLCursorPrepared
该类继承cursor.MySQLCursor,使用二进制协议执行prepare statement
使用方法:
import mysql.connector
from mysql.connector.cursor import MySQLCursorPrepared
cnx = mysql.connector.connect(database='employees')
cursor = cnx.cursor(cursor_class=MySQLCursorPrepared)
此时cursor为MySQLCursorPrepared对象。
举例:
cursor = cnx.cursor(cursor_class=MySQLCursorPrepared)
stmt = "SELECT fullname FROM employees WHERE id = ?" # (1)
cursor.execute(stmt, (5,)) # (2)
# ... fetch data ...
cursor.execute(stmt, (10,)) # (3)
# ... fetch data ...
Class constants.ClientFlag
This class provides constants defining MySQL client flags that can be used when the connection is established to configure the session.
>>> import mysql.connector
>>> mysql.connector.ClientFlag.FOUND_ROWS
2
Class constants.FieldType
该类不能被实例化,支持所有MySQL的数据类型。
from __future__ import print_function
import mysql.connector
from mysql.connector import FieldType
cnx = mysql.connector.connect(user='scott', database='test')
cursor = cnx.cursor()
cursor.execute(
"SELECT DATE(NOW()) AS `c1`, TIME(NOW()) AS `c2`, "
"NOW() AS `c3`, 'a string' AS `c4`, 42 AS `c5`")
rows = cursor.fetchall()
for desc in cursor.description:
colname = desc[0]
coltype = desc[1]
print("Column {} has type {}".format(
colname, FieldType.get_info(coltype)))
cursor.close()
cnx.close()
Class constants.SQLMode
提供所有已知的SQL服务器模式。具体参见 http://dev.mysql.com/doc/refman/5.6/en/server-sql-mode.html
Class constants.CharacterSet
提供MYSQL的字符集和默认的collations。参见Method MySQLConnection.set_charset_collation
Class constants.RefreshOption
该类提供多种flush的操作。
RefreshOption.GRANT
Refresh the grant tables, like FLUSH PRIVILEGES.
RefreshOption.LOG
Flush the logs, like FLUSH LOGS.
RefreshOption.TABLES
Flush the table cache, like FLUSH TABLES.
RefreshOption.HOSTS
Flush the host cache, like FLUSH HOSTS.
RefreshOption.STATUS
Reset status variables, like FLUSH STATUS.
RefreshOption.THREADS
Flush the thread cache.
RefreshOption.SLAVE
On a slave replication server, reset the master server information and restart the slave, like RESET SLAVE.
RefreshOption.MASTER
On a master replication server, remove the binary log files listed in the binary log index and truncate the index file, like RESET MASTER.
相关文章推荐
- MySQL Python教程(1)
- 使用Python发送邮件附件以定时备份MySQL的教程
- 实现MySQL回滚的Python脚本的编写教程
- MYSQL Python 入门教程
- MySQL Python教程(3)
- 在Python的框架中为MySQL实现restful接口的教程
- Windows7 x64下安装JDK+python+Django+MySql配置教程
- Python+Django+SAE系列教程13-----MySQL记录的添\删\改
- 在Windows系统上搭建Nginx+Python+MySQL环境的教程
- Linux下编译安装MySQL-Python教程
- 在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
- MYSQL Python 入门教程
- MYSQL Python 入门教程
- Python操作Mysql实例代码教程在线版(查询手册)_python
- Python+Django+SAE系列教程13-----MySQL记录的添\删\改
- 把MySQL表结构映射为Python中的对象的教程
- MySQL Python教程(3)
- python3操作mysql教程
- Python操作Mysql实例教程手册
- 用python简单实现mysql数据同步到ElasticSearch的教程