本文整理汇总了Python中MySQLdb.cursors方法的典型用法代码示例。如果您正苦于以下问题:Python MySQLdb.cursors方法的具体用法?Python MySQLdb.cursors怎么用?Python MySQLdb.cursors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MySQLdb
的用法示例。
在下文中一共展示了MySQLdb.cursors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_settings
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def from_settings(cls, settings):
"""
自定义组件或扩展很有用的方法: 这个方法名字固定, 是会被scrapy调用的。
这里传入的cls是指当前的class
"""
db_parms = dict(
host=settings["MYSQL_HOST"],
db=settings["MYSQL_DBNAME"],
user=settings["MYSQL_USER"],
passwd=settings["MYSQL_PASSWORD"],
charset='utf8mb4',
cursorclass=MySQLdb.cursors.DictCursor,
use_unicode=True,
)
# 连接池ConnectionPool
dbpool = adbapi.ConnectionPool("MySQLdb", **db_parms)
# 此处相当于实例化pipeline, 要在init中接收。
return cls(dbpool)
示例2: from_settings
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def from_settings(cls,settings):
'''1、@classmethod声明一个类方法,而对于平常我们见到的则叫做实例方法。
2、类方法的第一个参数cls(class的缩写,指这个类本身),而实例方法的第一个参数是self,表示该类的一个实例
3、可以通过类来调用,就像C.f(),相当于java中的静态方法'''
dbparams=dict(
host=settings['MYSQL_HOST'],#读取settings中的配置
db=settings['MYSQL_DBNAME'],
user=settings['MYSQL_USER'],
passwd=settings['MYSQL_PASSWD'],
charset='utf8',#编码要加上,否则可能出现中文乱码问题
cursorclass=MySQLdb.cursors.DictCursor,
use_unicode=False,
)
dbpool=adbapi.ConnectionPool('MySQLdb',**dbparams)#**表示将字典扩展为关键字参数,相当于host=xxx,db=yyy....
return cls(dbpool)#相当于dbpool付给了这个类,self中可以得到
#pipeline默认调用
示例3: conn_mysql
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def conn_mysql():
verbose("Connecting to MySQL......")
if args.prompt_password:
password=getpass.getpass("Please input your MySQL password:")
else:
password = args.password
conn = MySQLdb.connect(user=args.user,host=args.host,port=args.port,unix_socket=args.socket,passwd=args.password,db=args.old_database,cursorclass=MySQLdb.cursors.DictCursor)
return conn
# 建立MySQL连接
# connect to MySQL
示例4: connect_mysql
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def connect_mysql(instance, role='admin'):
"""Connect to a MySQL instance as admin
Args:
hostaddr - object describing which mysql instance to connect to
role - a string of the name of the mysql role to use. A bootstrap role can
be called for MySQL instances lacking any grants. This user does not
exit in zk.
Returns:
a connection to the server as administrator
"""
if role == 'bootstrap':
socket = host_utils.get_cnf_setting('socket', instance.port)
username = 'root'
password = ''
db = MySQLdb.connect(unix_socket=socket,
user=username,
passwd=password,
cursorclass=MySQLdb.cursors.DictCursor)
else:
username, password = get_mysql_user_for_role(role)
db = MySQLdb.connect(host=instance.hostname,
port=instance.port,
user=username,
passwd=password,
cursorclass=MySQLdb.cursors.DictCursor,
connect_timeout=CONNECT_TIMEOUT)
return db
示例5: _get_conn_config_mysql_client
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def _get_conn_config_mysql_client(self, conn):
conn_config = {
"user": conn.login,
"passwd": conn.password or '',
"host": conn.host or 'localhost',
"db": self.schema or conn.schema or ''
}
# check for authentication via AWS IAM
if conn.extra_dejson.get('iam', False):
conn_config['passwd'], conn.port = self.get_iam_token(conn)
conn_config["read_default_group"] = 'enable-cleartext-plugin'
conn_config["port"] = int(conn.port) if conn.port else 3306
if conn.extra_dejson.get('charset', False):
conn_config["charset"] = conn.extra_dejson["charset"]
if conn_config["charset"].lower() in ('utf8', 'utf-8'):
conn_config["use_unicode"] = True
if conn.extra_dejson.get('cursor', False):
import MySQLdb.cursors
if (conn.extra_dejson["cursor"]).lower() == 'sscursor':
conn_config["cursorclass"] = MySQLdb.cursors.SSCursor
elif (conn.extra_dejson["cursor"]).lower() == 'dictcursor':
conn_config["cursorclass"] = MySQLdb.cursors.DictCursor
elif (conn.extra_dejson["cursor"]).lower() == 'ssdictcursor':
conn_config["cursorclass"] = MySQLdb.cursors.SSDictCursor
local_infile = conn.extra_dejson.get('local_infile', False)
if conn.extra_dejson.get('ssl', False):
# SSL parameter for MySQL has to be a dictionary and in case
# of extra/dejson we can get string if extra is passed via
# URL parameters
dejson_ssl = conn.extra_dejson['ssl']
if isinstance(dejson_ssl, str):
dejson_ssl = json.loads(dejson_ssl)
conn_config['ssl'] = dejson_ssl
if conn.extra_dejson.get('unix_socket'):
conn_config['unix_socket'] = conn.extra_dejson['unix_socket']
if local_infile:
conn_config["local_infile"] = 1
return conn_config
示例6: __init__
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def __init__(self, host, port, max_idle_time=8*3600, connect_timeout=3, **kwargs):
#打开数据库连接
self.db = None
self.host = host
self.port = port
self.max_idle_time = float(max_idle_time)
self.last_use_time = time.time()
self.kwargs = dict(host=host, port=port, charset="utf8", connect_timeout=connect_timeout, cursorclass=MySQLdb.cursors.DictCursor, **kwargs)
try:
self.connect()
except Exception:
raise
示例7: initialise_database
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def initialise_database(self):
self._db_client = MySQLdb.connect ( self._mysql_host,
self._mysql_username,
self._mysql_password,
self._mysql_db,
cursorclass = MySQLdb.cursors.DictCursor
)
self._influx_client = InfluxDBClient(
self._influx_db_host,
self._influx_db_port,
self._influx_db_username,
self._influx_db_password,
self._influx_db
)
示例8: create_connection
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def create_connection(self):
conn = MySQLdb.connect(
host=self.config['AIRFLOW_DB_HOST'],
user=self.config['AIRFLOW_USERNAME'],
password=self.config['AIRFLOW_PASSWORD'],
db=self.config['AIRFLOW_DATABASE'],
cursorclass=MySQLdb.cursors.DictCursor,
connect_timeout=3
)
conn.autocommit(True)
return conn
示例9: get_cursor
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def get_cursor(self, ctype=MySQLdb.cursors.Cursor):
self.ensure_connect()
return self._connect.cursor(ctype)
示例10: process
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def process(args):
read_conn = get_mysql_conn(args)
write_conn = get_mysql_conn(args)
read_sql = READ_SQL.format(**vars(args))
update_sql = UPDATE_SQL.format(**vars(args))
read_cur = MySQLdb.cursors.SSCursor(read_conn)
read_cur.execute(read_sql)
for row in read_cur:
pkey, text = row
lemmatized = lemmatize(text)
write_cur = write_conn.cursor()
write_cur.execute(update_sql, (lemmatized, pkey))
write_conn.commit()
示例11: __init__
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def __init__(self):
self.db = MySQLdb.connect("localhost", "root", "199194", "tianchi",
cursorclass = MySQLdb.cursors.DictCursor)
self.cursor = self.db.cursor()
示例12: __init__
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def __init__(self):
self.db = MySQLdb.connect("localhost", "root", "199194", "tianchi",
cursorclass = MySQLdb.cursors.DictCursor)
self.cursor = self.db.cursor()
# 这个数据库连接用来查询train_user_after 31th的数据
self.db2 = MySQLdb.connect("localhost", "root", "199194", "tianchi",
cursorclass = MySQLdb.cursors.DictCursor)
self.cursor2 = self.db2.cursor()
示例13: from_settings
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def from_settings(cls, settings):
dbparams = dict(
host = settings['HOST_ADDRESS'],
db = settings['DB_NAME'],
user = settings['USER'],
passwd = settings['PASSWORD'],
charset = 'utf8', # 编码要加上,否则可能出现中文乱码问题
cursorclass = MySQLdb.cursors.DictCursor,
use_unicode = False,
)
dbpool = adbapi.ConnectionPool('MySQLdb', **dbparams) # **表示将字典扩展为关键字参数,相当于host=xxx,db=yyy....
return cls(dbpool) # 相当于dbpool付给了这个类,self中可以得到
# pipeline默认调用
示例14: from_settings
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def from_settings(cls, settings):
dbparms = dict(
host=settings["MYSQL_HOST"],
db=settings["MYSQL_DBNAME"],
user=settings["MYSQL_USER"],
passwd=settings["MYSQL_PASSWORD"],
charset='utf8',
cursorclass=MySQLdb.cursors.DictCursor,
use_unicode=True,
)
dbpool = adbapi.ConnectionPool("MySQLdb", **dbparms)
return cls(dbpool)
示例15: _conn_db
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import cursors [as 别名]
def _conn_db(self, host, user, passwd, db):
return MySQLdb.connect(host=host,
user=user,
passwd=passwd,
db=db,
cursorclass=MySQLdb.cursors.DictCursor)