当前位置: 首页>>代码示例>>Python>>正文


Python MySQLConnection.connect方法代码示例

本文整理汇总了Python中mysql.connector.MySQLConnection.connect方法的典型用法代码示例。如果您正苦于以下问题:Python MySQLConnection.connect方法的具体用法?Python MySQLConnection.connect怎么用?Python MySQLConnection.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mysql.connector.MySQLConnection的用法示例。


在下文中一共展示了MySQLConnection.connect方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1:

# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import connect [as 别名]
'''
Created on Jul 16, 2016

@author: weizhenyuan
'''
from mysql.connector import MySQLConnection
MySQLConnection.connect()

print
开发者ID:zhenyuanwei,项目名称:healthcare,代码行数:11,代码来源:test.py

示例2: __init__

# 需要导入模块: from mysql.connector import MySQLConnection [as 别名]
# 或者: from mysql.connector.MySQLConnection import connect [as 别名]
class MySqlAccess:
    """
    Wrapper class to access MySQL DB using mysql connector/python
    """

    def __init__(self, db_host=default_host, db_port=default_port):
        self.__init_consts()
        self.__invalid_db = False
        self.__order = self.__SQL_ORDER_DESC
        try:
            self._conn = MySQLConnection(host=db_host,
                                         port=db_port)
        except errors.InterfaceError:
            self.__invalid_db = True

    def __init_consts(self):
        # SQL
        self.__SQL_SHOW_DB = 'SHOW DATABASES'
        self.__SQL_SHOW_TABLES = 'SHOW TABLES IN '
        self.__SQL_SHOW_COLUMNS_FMT = 'SHOW COLUMNS IN {0} IN {1}'
        self.__SQL_USE_DATABASE = 'USE '
        self.__SQL_ORDER_DESC = ' DESC'
        self.__SQL_ORDER_ASC = ' ASC'
        self.__SQL_SELECT = 'SELECT '
        self.__SQL_COLUMN_SEP = ', '
        self.__SQL_FROM = ' FROM '
        self.__SQL_DOT = '.'
        self.__SQL_ORDER_BY = ' ORDER BY '
        self.__SQL_UPDATE = 'UPDATE {0} SET '
        self.__SQL_WHERE = ' WHERE '
        self.__SQL_ASSIGN = '='
        self.__SQL_DELETE = 'DELETE FROM {0}'
        self.__SQL_INSERT = 'INSERT INTO {0}('
        self.__SQL_INSERT_VALUES = ') VALUES ('
        self.__SQL_INSERT_END = ')'
        self.__SQL_OPEN_BRACKET = '['
        self.__SQL_CLOSE_BRACKET = ']'
        self.__SQL_VALUE = 'value-'

        # SQL templates
        self.SQL_TEMPLATE_SELECT = 'SELECT {0} FROM {1} WHERE 1'

    def connect(self, user_name=None, user_password=None) -> bool:
        if (user_name is None) or (user_password is None) or self.__invalid_db:
            return False
        try:
            self._conn.connect(user=user_name, password=user_password)
        except errors.ProgrammingError:
            return False
        return True

    def disconnect(self):
        self._conn.close()

    def query(self, sql_stmt=None) -> [list]:
        cursor = self._conn.cursor()
        query = sql_stmt
        try:
            cursor.execute(query)
            l = [row for row in cursor]
        except errors.ProgrammingError:
            return None
        cursor.close()
        return l

    def get_database(self) -> [list]:
        return self.query(self.__SQL_SHOW_DB)

    def get_tables(self, db_name) -> [list]:
        return self.query(self.__SQL_SHOW_TABLES + db_name)

    def get_columns_of_table(self, db_name, table_name) -> [list]:
        return self.query(self.__SQL_SHOW_COLUMNS_FMT.format(table_name, db_name))

    def flip_order(self):
        if self.__order == self.__SQL_ORDER_DESC:
            self.__order = self.__SQL_ORDER_ASC
        else:
            self.__order = self.__SQL_ORDER_DESC

    def compose_select(self, db_name, table_name, columns, order_column) -> str:
        sql = self.__SQL_SELECT
        for v in columns:
            sql += v + self.__SQL_COLUMN_SEP
        query = self.__remove_trailing_sep(sql)
        query += self.__SQL_FROM + db_name + self.__SQL_DOT + table_name
        query += self.__SQL_ORDER_BY + order_column + self.__order
        return query

    def compose_update(self, db_name, table_name, columns) -> str:
        sql = self.__SQL_UPDATE.format(db_name + self.__SQL_DOT + table_name)
        for (i, v) in zip(range(len(columns)), columns):
            sql += v + self.__SQL_ASSIGN
            sql += self.__SQL_OPEN_BRACKET + self.__SQL_VALUE + str(i + 1) + self.__SQL_CLOSE_BRACKET
            sql += self.__SQL_COLUMN_SEP
        query = self.__remove_trailing_sep(sql)
        query += self.__SQL_WHERE
        return query

    def compose_delete(self, db_name, table_name) -> str:
#.........这里部分代码省略.........
开发者ID:raof01,项目名称:utilities,代码行数:103,代码来源:MySqlAccess.py


注:本文中的mysql.connector.MySQLConnection.connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。