How to use the pymysql.cursors.DictCursor function in PyMySQL

To help you get started, we’ve selected a few PyMySQL examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github twindb / proxysql-tools / tests / integration / galera / test_set_online.py View on Github external
['--config', config_file, 'galera', 'server', 'set_offline', percona_xtradb_cluster_three_node[2]['ip'], '3306']
                           )
    assert result.exit_code == 0

    result = runner.invoke(main,
                           ['--config', config_file, 'galera', 'register']
                           )
    assert result.exit_code == 0

    connection = pymysql.connect(
        host=proxysql_instance.host,
        port=proxysql_instance.port,
        user=proxysql_instance.user,
        passwd=proxysql_instance.password,
        connect_timeout=20,
        cursorclass=DictCursor)

    try:
        with connection.cursor() as cursor:
            cursor.execute('SELECT `hostgroup_id`, `hostname`, '
                           '`port`, `status`, `weight`, `compression`, '
                           '`max_connections`, `max_replication_lag`, '
                           '`use_ssl`, `max_latency_ms`, `comment`'
                           ' FROM `mysql_servers`'
                           ' WHERE hostgroup_id = %s'
                           ' AND hostname = %s',
                           (
                               hostgroup_reader,
                               percona_xtradb_cluster_three_node[2]['ip']
                           )
                           )
            row = cursor.fetchall()[0]
github twindb / proxysql-tools / tests / integration / galera / test_set_online.py View on Github external
result = runner.invoke(main,
                           ['--config', config_file, 'galera', 'server', 'set_online', percona_xtradb_cluster_three_node[2]['ip'], '3306']
                           )
    assert result.exit_code == 0
    result = runner.invoke(main,
                           ['--config', config_file, 'galera', 'register']
                           )
    assert result.exit_code == 0

    connection = pymysql.connect(
        host=proxysql_instance.host,
        port=proxysql_instance.port,
        user=proxysql_instance.user,
        passwd=proxysql_instance.password,
        connect_timeout=20,
        cursorclass=DictCursor)

    try:
        with connection.cursor() as cursor:
            cursor.execute('SELECT `hostgroup_id`, `hostname`, '
                           '`port`, `status`, `weight`, `compression`, '
                           '`max_connections`, `max_replication_lag`, '
                           '`use_ssl`, `max_latency_ms`, `comment`'
                           ' FROM `mysql_servers`'
                           ' WHERE hostgroup_id = %s'
                           ' AND hostname = %s',
                           (
                               hostgroup_reader,
                               percona_xtradb_cluster_three_node[2]['ip']
                           )
                           )
            row = cursor.fetchall()[0]
github thinkdb / MySQL_Diagnose / lib / funcs.py View on Github external
def __init__(self, host, user, password, port, database=None, auto_commit=0):
        self.host = host
        try:
            self.conn = pymysql.connect(host=self.host, user=user, passwd=password,
                                        port=int(port), charset='utf8mb4',
                                        )
            if database:
                self.conn.select_db(database)

            if auto_commit:
                self.conn.autocommit(auto_commit)
            self.cur = self.conn.cursor(pymysql.cursors.DictCursor)
        except Exception as e:
            Logger().logger(30).error('{ip}: '.format(ip=host) + str(e))
            sys.exit(str(e))
github DingGuodong / LinuxBashShellScriptForOps / functions / nosql / database / mysql / pyMysqlLoginMonitor.py View on Github external
def mysql_query(sql):
    """
    mysql query sql on mysql server
    :param sql:  SQL
    :type sql: str
    :return result:  return all result with fetchall()
    :type: tuple
    """
    import pymysql
    try:
        connection = pymysql.connect(host='localhost', user='dev', passwd='dEvp@ssw0rd', db='test', port=3306,
                                     charset='utf8',
                                     read_timeout=10, write_timeout=10,
                                     cursorclass=pymysql.cursors.DictCursor)
    except pymysql.err.OperationalError as e:
        log.error("%s %s" % (e.args, e.message))
        sys.exit(1)

    try:
        with connection.cursor() as cursor:
            cursor.execute(sql)
        connection.commit()
    finally:
        connection.close()
    if cursor is not None:
        # fetchall() return a tuple
        return cursor.fetchall()
    else:
        print('This will never be reached when joined gevent')
        sys.exit(1)
github hequan2017 / autoops / tasks / views.py View on Github external
def sql_rb(user, password, host, port, sequence, backup_dbname):  ##   查询回滚语句的表格

    port_a = int(port)
    connection = pymysql.connect(host=host, port=port_a, user=user, password=password, db=backup_dbname, charset="utf8",
                                 cursorclass=pymysql.cursors.DictCursor)
    connection1 = pymysql.connect(host=host, port=port_a, user=user, password=password, db=backup_dbname,
                                  charset="utf8",
                                  cursorclass=pymysql.cursors.DictCursor)

    try:
        with connection.cursor() as cursor:

            sql = " select  tablename   from  {0}.{1}  where opid_time ='{2}' ;".format(backup_dbname,  '$_$inception_backup_information$_$',sequence)
            cursor.execute(sql)
            result = cursor.fetchone()
            connection.commit()
            connection.close()
        table = result['tablename']

        try:
            with connection1.cursor() as cursor1:
github wingjay / jianshi / server / server / db / diary.py View on Github external
def create_diary(user_id, uuid, title, content, time_created=None):
    """
    Raise: errors.DbCreateError
    """
    if user_id is None or uuid is None or title is None or content is None:
        raise errors.ArgumentShouldNotBeNull()

    _user = db_user.get_user(user_id)
    if _user is None:
        raise errors.InvalidUserIdDuringCreatingDiary()

    conn = base_db.get_conn(pymysql.cursors.DictCursor)
    if time_created is None:
        time_created = int(time.time())
    new_diary_id = -1
    try:
        with conn.cursor() as cursor:
            sql = "insert into `Diary` (`user_id`, `uuid`, `title`, `content`, `time_created`, `time_modified`) " \
                  "values (%s, %s, %s, %s, %s, %s)"
            cursor.execute(sql, (str(user_id), str(uuid), str(title), str(content), str(time_created), str(time_created)))
            new_diary_id = cursor.lastrowid
        conn.commit()
    except Exception as e:
        logger.exception(e)
        raise errors.DbCreateError()
    finally:
        conn.close()
        return new_diary_id
github anycodes / AI_Album / scf / get_album_infor / get_album_infor.py View on Github external
# -*- coding: utf8 -*-

import pymysql
import json

connection = pymysql.connect(host="切换为自己的数据库host",
                             user="root",
                             password="切换为自己的数据库密码",
                             port=int(62580),
                             db="mini_album",
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor,
                             autocommit=1)


def getUserInfor(connection, wecaht):
    try:
        connection.ping(reconnect=True)
        cursor = connection.cursor()
        search_stmt = (
            "SELECT * FROM `users` WHERE `wechat`=%s"
        )
        data = (wecaht)
        cursor.execute(search_stmt, data)
        cursor.close()
        result = cursor.fetchall()
        if len(result) == 1:
            result = result[0]
github cyberkitsune / PSO2Proxy / proxy / plugins / disabled / redpill.py View on Github external
def get_connection():
    global jsonConfig
    db_settings = jsonConfig['dbConfig']
    connection = pymysql.connect(host=db_settings['host'], port=db_settings['port'], db=db_settings['db'],
                                 user=db_settings['user'], passwd=db_settings['passwd'], cursorclass=pymysql.cursors.DictCursor)
    return connection
github tencentyun / scf-demo-repo / Python2.7-MySQL / mysql_basic_demo.py View on Github external
def connect_mysql():
    global g_connection
    global g_connection_errno
    try:
        g_connection = pymysql.connect(host=Host,
                                     user=User,
                                     password=Password,
                                     port=Port,
                                     db=DB,
                                     charset='utf8',
                                     cursorclass=pymysql.cursors.DictCursor)
    except Exception as e:
        g_connection = None
        g_connection_errno = e[0]
        print("connect database error:%s"%e)
github Mausbiber / smartHome / libs / db.py View on Github external
def query(self, sql, *arguments):
        try:
            cursor = self.conn.cursor(pymysql.cursors.DictCursor)
            cursor.execute(sql, arguments)
        except (AttributeError, pymysql.err.OperationalError):
            self.connect()
            cursor = self.conn.cursor(pymysql.cursors.DictCursor)
            cursor.execute(sql, arguments)
        return cursor