美文网首页Python小推车python学习
Python学习打call第四十七天:mysqlclient操作

Python学习打call第四十七天:mysqlclient操作

作者: 暖A暖 | 来源:发表于2019-03-25 17:25 被阅读12次

    1.mysqlclient基本使用

    • 安装mysqlclient的命令行:pip install mysqlclient
    • 在创建数据库,创建一张表;
    create database XKD_Python_Course;
    use XKD_Python_Course;
    CREATE TABLE `students` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `age` int(11) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    
    • 授权用户:
    grant [权限的名称 select insert ... | all] on 数据库.表名 to 用户名@主机地址 with grant option;
    flush privileges;
    

    2.什么是cursor

    • cursor是游标对象,用于执行查询和获取结果;

    3.游标支持的方法有

    • execute(op[,args]):执行一个数据库的查询和命令;

    • fetchmany(size):获取结果集的下几行务;

    • fetchone() :获取结果集的下一行;

    • fetchall():获取结果集中剩下的所有行;

    • rowcount():最近一次execute返回数据的行数或影响的行数;

    • close():关闭游标对象;

    4.插入操作

    import MySQLdb
    connect = None
    cursor = None
    try:
        connect = MySQLdb.connect(host='localhost',
                                      user='Mark',
                                      password='Mark123456',
                                      database='Student',
                                      use_unicode=True,
                                      charset='utf8')
        cursor = connect.cursor()
    except Exception as e:
        print(e)
        connect.close()
    try:
        if cursor:
            result = cursor.execute("insert into students (name, age) values ('robby', 27)")
            print('result = {}'.format(result))
            connect.commit()
    except Exception as e:
        print(e)
        connect.rollback()
    finally:
        if cursor:
            cursor.close()
        if connect:
            connect.close()
    

    5.查询数据

    import MySQLdb
    from pprint import pprint
    connect = None
    cursor = None
    try:
        connect = MySQLdb.connect(host='localhost',
                                      user='robby',
                                      password='Mark123456',
                                      database='Student',
                                      use_unicode=True,
                                      charset='utf8')
        cursor = connect.cursor()
    except Exception as e:
        print(e)
        connect.close()
    try:
        if cursor:
            cursor.execute('select * from students')
            one_result = cursor.fetchone()
            many_result = cursor.fetchmany(9)
            all_result = cursor.fetchall()
            pprint(one_result)
            pprint(many_result)
            pprint(all_result)
    except Exception as e:
        print(e)
        connect.rollback()
    finally:
        if cursor:
            cursor.close()
        if connect:
            connect.close()
    

    6.使用上下文管理

    import MySQLdb
    connect = MySQLdb.connect(host='localhost',
                              user='robby',
                              password='Mark123456',
                              database='Student',
                              use_unicode=True,
                              charset='utf8')
    with connect as cursor:
        # 如果这里对cursor做上下文管理,当退出cursor的上下文管理,会自动关闭cursor对象
        cursor.execute("insert into students (name, age) values ('robby', 27)")
    # 此时连接还没有关闭
    cursor.execute("insert into students (name, age) values ('robby', 28)")
    connect.close()
    

    7.Queue模块

    ##############queue模块中的Queue
    from threading import Thread, Event
    from queue import Queue
    import time
    def write(q: Queue, e: Event):
        for value in range(100):
            print('put {} to queue'.format(value))
            q.put(value)
            time.sleep(0.5)
        else:
            e.set()
    def read(q: Queue, e: Event):
        while True :
            if not q.empty() or not e.is_set():
                value = q.get()
                print('get {} from queue'.format(value))
                time.sleep(1)
            else:
                break
    if __name__ == '__main__':
        q = Queue()
        e = Event()
        tw = Thread(target=write, args=(q,e))
        tr = Thread(target=read, args=(q,e))
        tw.start()
        tr.start()
        tw.join()
        tr.join()
        print('finished ')
    
    
    ############multiprocessing模块的queue
    from multiprocessing import Process,Queue, Event
    import time
    def write(q: Queue, e: Event):
        for value in range(100):
            print('put {} to queue'.format(value))
            q.put(value)
            time.sleep(0.5)
        else:
            e.set()
    def read(q: Queue, e: Event):
        while True :
            if not q.empty() or not e.is_set():
                value = q.get()
                print('get {} from queue'.format(value))
                time.sleep(1)
            else:
                break
    if __name__ == '__main__':
        q = Queue()
        e = Event()
        pw = Process(target=write, args=(q,e))
        pr = Process(target=read, args=(q,e))
        pw.start()
        pr.start()
        pw.join()
        pr.join()
        print('finished ')
    
    

    Queue模块实现了多生产者多消费者队列,尤其适合多线程编程,Queue类中实现了所有需要的锁原语,Queue模块实现了三种类型队列:

    • 第一种,FIFO(先进先出)队列,第一加入队列的任务,被第一个取出;

    • 第二种,LIFO(后进先出)队列,最后加入队列的任务,被第一个取出(操作类似与栈,总是从栈顶取出,这个队列还不清楚内部的实现);

    • 第三种,PriorityQueue(优先级)队列,保持队列数据有序,最小值被先取出;

    8.Queue队列对象的方法

    • qsize():返回队列的大致大小;

    • empty():判断队列是否为空,如果队列为空,返回True,反之False;

    • full():判断是否满了;

    • put():将项目放入队列;

    • put_nowait:相当于put(item, False);

    • get():从队列中删除并返回一个项目;

    • get_nowait():提供了两种方法来支持跟踪守护进程消费者线程是否已完全处理入队任务;

    • task_done():表示以前排队的任务已完成;

    • join():阻止直到队列中的所有项目都已获取并处理完毕;

    9.使用Queue构建连接池

    from queue import Queue
    import MySQLdb
    class ConnectPool:
        def __init__(self, size=5, *args, **kwargs):
            if not isinstance(size, int) or size < 1:
                size = 10
            self.__pool = Queue(size)
            for i in range(size):
                self.__pool.put(MySQLdb.connect(*args, **kwargs))
        @property
        def connect(self):
            return self.__pool.get()
        @connect.setter
        def connect(self, conn):
            self.__pool.put(conn)
    if __name__ == '__main__':
        # 构建连接池
        pool = ConnectPool(host='localhost',
                           user='robby',
                           password='robby123456',
                           database='XKD_Python_Course',
                           use_unicode=True,
                           charset='utf8')
        # 获取一个连接
        connect = pool.connect
        #
        with connect as cursor:
            with cursor:
                sql = 'select * from students'
                cursor.execute(sql)
                print(cursor.fetchall())
    

    10.线程连接池实现

    from queue import Queue
    import MySQLdb
    import threading
    class ConnectPool:
        def __init__(self, size=5, *args, **kwargs):
            if not isinstance(size, int) or size < 1:
                size = 10
            self.__pool = Queue(size)
            for i in range(size):
                self.__pool.put(MySQLdb.connect(*args, **kwargs))
            # 创建一个local对象
            self.local = threading.local()
        @property
        def connect(self):
            return self.__pool.get()
        @connect.setter
        def connect(self, conn):
            self.__pool.put(conn)
        def __enter__(self):
            if getattr(self.local, 'conn', None) is None:
                self.local.conn = self.connect
            return self.local.conn.cursor()
        def __exit__(self, *exc_info):
            if exc_info:
                self.local.conn.rollback()
            else:
                self.local.conn.commit()
            # 将连接对象归还到连接池
            self.connect = self.local.conn
            # 线程级别的连接对象引用计算器减一
            self.local.conn = None
    if __name__ == '__main__':
        pool = ConnectPool(host='localhost',
                           user='robby',
                           password='robby123456',
                           database='XKD_Python_Course',
                           use_unicode=True,
                           charset='utf8')
        def foo(pool):
            with pool as cursor:
                with cursor:  # 对cursor做上下文管理
                    sql = 'select * from students'
                    cursor.execute(sql)
                    print(cursor.fetchall())
        for i in range(5):
            t = threading.Thread(target=foo, args=(pool, ))
            t.start()
    

    11.queue模块中的Queue与multiprocessing模块的Queue的区别

    • queue模块中的Queue:是普通的队列模式,先进先出模式,get方法会阻塞请求,直到有数据get出来为止,适用于多线程的场景;

    • multiprocessing模块的Queue:是多进程并发的Queue队列,用于解决多进程间的通信问题;

    参考:https://www.9xkd.com/user/plan-view.html?id=2546175346

    相关文章

      网友评论

        本文标题:Python学习打call第四十七天:mysqlclient操作

        本文链接:https://www.haomeiwen.com/subject/lxamvqtx.html