本文整理匯總了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)