Python与Mysql交互

作者: Jayss_987 | 来源:发表于2019-02-12 23:56 被阅读163次

    其实python可以做很多事情,例如可以连接我们熟悉的数据库——Mysql。
    下面就由我为大家介绍,如何用python连接Mysql数据库

    python连接mysql的基本原理:

    1. 导入模块:import pymysql
    2. 建立连接:pymysql.connect(**登录对象)
    3. 创建游标:connection.curxor()
    4. 执行SQL语句:cursor.execute(SQL语句)
    5. 获取结果
      PS:切记在最后要关闭游标和连接

    连接时要注意的地方:

    1. 在python中执行SQL语句不用加分号(;)
    2. execute执行完后并不是直接的结果,需主动获取
    3. 与执行文件一样,要关闭游标与连接
    4. 注意食物的回滚与提交(rollback、commit)

    实战:

    为大家演示的是python连接mysql数据库
    并执行:“select * from emp”

    1. 下面是在linux系统中执行的SQL语句,并附带结果:

    在linux系统中查看emp表的内容
    mysql> select * from emp;
    +-------+--------+-----------+------+------------+---------+---------+--------+
    | empno | ename  | job       | mgr  | hiredate   | sal     | comm    | deptno |
    +-------+--------+-----------+------+------------+---------+---------+--------+
    |  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |    NULL |     20 |
    |  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-02 | 1600.00 |  300.00 |     30 |
    |  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
    |  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 |
    |  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
    |  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 |    NULL |     30 |
    |  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 |    NULL |     10 |
    |  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 |    NULL |     10 |
    |  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
    |  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |    NULL |     30 |
    |  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |    NULL |     20 |
    |  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 |
    +-------+--------+-----------+------+------------+---------+---------+--------+
    12 rows in set (0.01 sec)
    
    

    2. 下面是在python中执行SQL语句的方法演示:

    #导入pymysql模块
    import pymysql
    
    #创建登录细信息对象:db_login
    db_login = {
        'user':'root',              #用户名
        'password':'Passw0rd',      #数据库密码
        'db':'DB',                  #数据库名
        'charset':'utf8'            #注意不是utf-8
    }
    
    #创建连接mysql对象:link
    link = pymysql.connect(**db_login)
    
    #创建游标对象:cursor(利用游标来执行SQL语句)
    cursor = link.cursor()
    
    try:
        #执行SQL语句,不反悔结果,返回其影响的行数
        executes = cursor.execute("select * from emp")
    
        #循环打印结果
        for value in cursor.fetchall():
            print(value)
    
        #提交到数据库,真正把数据插入或者更新到数据中
        link.commit()
    
    except Exception as error:
        print(error)
        
        #发生异常、回滚
        link.rollback()
    
    finally:
        
        #关闭游标
        cursor.close()
        
        #关闭连接
        link.close()
    
    

    3. python执行SQL语句的结果

    (Decimal('7369'), 'SMITH', 'CLERK', Decimal('7902'), datetime.date(1980, 12, 17), Decimal('800.00'), None, Decimal('20'))
    (Decimal('7499'), 'ALLEN', 'SALESMAN', Decimal('7698'), datetime.date(1981, 2, 2), Decimal('1600.00'), Decimal('300.00'), Decimal('30'))
    (Decimal('7521'), 'WARD', 'SALESMAN', Decimal('7698'), datetime.date(1981, 2, 22), Decimal('1250.00'), Decimal('500.00'), Decimal('30'))
    (Decimal('7566'), 'JONES', 'MANAGER', Decimal('7839'), datetime.date(1981, 4, 2), Decimal('2975.00'), None, Decimal('20'))
    (Decimal('7654'), 'MARTIN', 'SALESMAN', Decimal('7698'), datetime.date(1981, 9, 28), Decimal('1250.00'), Decimal('1400.00'), Decimal('30'))
    (Decimal('7698'), 'BLAKE', 'MANAGER', Decimal('7839'), datetime.date(1981, 5, 1), Decimal('2850.00'), None, Decimal('30'))
    (Decimal('7782'), 'CLARK', 'MANAGER', Decimal('7839'), datetime.date(1981, 6, 9), Decimal('2450.00'), None, Decimal('10'))
    (Decimal('7839'), 'KING', 'PRESIDENT', None, datetime.date(1981, 11, 17), Decimal('5000.00'), None, Decimal('10'))
    (Decimal('7844'), 'TURNER', 'SALESMAN', Decimal('7698'), datetime.date(1981, 9, 8), Decimal('1500.00'), Decimal('0.00'), Decimal('30'))
    (Decimal('7900'), 'JAMES', 'CLERK', Decimal('7698'), datetime.date(1981, 12, 3), Decimal('950.00'), None, Decimal('30'))
    (Decimal('7902'), 'FORD', 'ANALYST', Decimal('7566'), datetime.date(1981, 12, 3), Decimal('3000.00'), None, Decimal('20'))
    (Decimal('7934'), 'MILLER', 'CLERK', Decimal('7782'), datetime.date(1982, 1, 23), Decimal('1300.00'), None, Decimal('10'))
    
    Process finished with exit code 0
    
    

    欢迎技术交流

    WeChat......

    相关文章

      网友评论

        本文标题:Python与Mysql交互

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