zoukankan      html  css  js  c++  java
  • Python学习笔记(十五)—操作数据库

    我们在工作中写代码的时候,经常会操作数据库,这里就介绍一下python怎么操作mysql数据库。

    python3中操作mysql数据库需要安装一个第三方模块,pymysql;在python2中是MySQLdb模块,在python3中没有MySQLdb模块了,所以使用pymysql。

    一、pymysql的安装

    第三方模块的安装
    1、傻瓜式安装,使用Python自带的pip命令进行安装
    2、手动安装
      .tar结尾的安装:
        1、先解压文件,解压后进入到这个目录下
        2、执行:Python setup.py install方式安装
      .whl结尾的安装:
        直接使用pip install C:XXXXXXXX.whl 文件(文件绝对路径/相对路径)
    注意:当电脑中存在多个版本时,当安装模块时,可以使用PythonX.X -m -pip install XXX 的方式来安装文件到指定的Python中

    详细参考:https://www.cnblogs.com/beginner-boy/p/7247688.html

    二、数据库的基本操作—增、删、改、查

    1、Python操作MySQL的基本步骤:

      1、建立数据库连接

      2、通过cursor()创建游标对象(pymysql通过游标来执行sql和获取结果)

      3、使用execute(),执行SQL语句

      4、获取结果(查询)/提交事务(增、删、改)

      5、关闭游标

      6、关闭连接

    注意:用Python进行增删改查操作之前,最好先检查下sql语句是否正确,确保没有错误之后,再放到Python代码中。

    2、数据库的查询

    def select_db(sql):
        '''查询模块'''
        #建立数据库的连接
        db = pymysql.connect(host = '192.168.0.105',user = 'root',password = '123456',db = 'szz',port = 3306,charset='utf8')
        cur = db.cursor()  #通过 cursor() 创建游标对象
        cur.execute(sql) #使用 execute() 执行sql
        data = cur.fetchall() #获取数据库里面的所有结果,返回一个二维数组
        cur.close() # 关闭游标
        db.close() # 关闭数据库连接
        return data
    select_sql = select_db("select * from stu;")
    print(select_sql)
    
    运行结果:
    ((1, 'test00'), (2, 'test'), (3, 'test1'))  #以元组嵌套的形式返回值
    
    说明:如果想以列表嵌套字典的形式返回则在创建游标时,添加如下参数即可:cursor=pymysql.cursors.DictCursor,即:cur = db.cursor(cursor=pymysql.cursors.DictCursor)
    
    def select_db(sql):
        '''查询模块'''
        #建立数据库的连接
        db = pymysql.connect(host = '192.168.0.105',user = 'root',password = '123456',db = 'szz',port = 3306,charset='utf8')
        # cur = db.cursor()  #通过 cursor() 创建游标对象
        cur = db.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql) #使用 execute() 执行sql
        data = cur.fetchall() #获取数据库里面的所有结果,返回一个二维数组
        cur.close() # 关闭游标
        db.close() # 关闭数据库连接
        return data
    select_sql = select_db("select * from stu;")
    print(select_sql)
    
    运行结果:
    [{'id': 1, 'name': 'test00'}, {'id': 2, 'name': 'test'}, {'id': 3, 'name': 'test1'}]

    注意:

    fetchall()函数是获取数据库里面的所有结果值;

    fetchone()函数则是获取数据库查询结果的第一条数据

    fetchmany(n)函数则是获取前N行的数据

    在实际应用中当查询的数据条数很多的时候使用fetchall最合适,如果查询结果只有一条则用fetchone比较合适

    3、数据库的更新操作

    def select_db(sql):
        '''查询模块'''
        #建立数据库的连接
        db = pymysql.connect(host = '192.168.0.105',user = 'root',password = '123456',db = 'szz',port = 3306,charset='utf8')
        # cur = db.cursor()  #通过 cursor() 创建游标对象
        cur = db.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql) #使用 execute() 执行sql
        db.commit()
        cur.close() # 关闭游标
        db.close() # 关闭数据库连接
    
    select_sql = select_db("update stu set name='test001' where name ='test00';")
    
    方式二:
    def select_db1(sql):
        '''查询模块'''
        #建立数据库的连接
        db = pymysql.connect(host = '192.168.0.105',user = 'root',password = '123456',db = 'szz',port = 3306,charset='utf8',autocommit=True)
        # cur = db.cursor()  #通过 cursor() 创建游标对象
        cur = db.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql) #使用 execute() 执行sql
        cur.close() # 关闭游标
        db.close() # 关闭数据库连接
    select_sql = select_db1("update stu set name='test00' where name ='test001';")
    
    #说明:在连接数据库的时候,如果参数中加入autocommit=True,那么在执行完成SQL语句后不用在进行commit操作

    4、数据库的插入

    def select_db1(sql):
        '''查询模块'''
        #建立数据库的连接
        db = pymysql.connect(host = '192.168.0.105',user = 'root',password = '123456',db = 'szz',port = 3306,charset='utf8',autocommit=True)
        # cur = db.cursor()  #通过 cursor() 创建游标对象
        cur = db.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql) #使用 execute() 执行sql
        cur.close() # 关闭游标
        db.close() # 关闭数据库连接
    select_sql = select_db1("insert into stu(id,name)value(5,'test3');")

    5、数据库数据的删除

    def select_db1(sql):
        '''查询模块'''
        #建立数据库的连接
        db = pymysql.connect(host = '192.168.0.105',user = 'root',password = '123456',db = 'szz',port = 3306,charset='utf8',autocommit=True)
        # cur = db.cursor()  #通过 cursor() 创建游标对象
        cur = db.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql) #使用 execute() 执行sql
        cur.close() # 关闭游标
        db.close() # 关闭数据库连接
    select_sql = select_db1("delete from stu where name='test00';")

    参考链接:

    https://www.cnblogs.com/wintest/p/12152687.html

    http://www.nnzhp.cn/archives/510

  • 相关阅读:
    23种设计模式目录总览
    Unity3d优化总结2
    Unity3d优化总结1
    Unity四元数和旋转
    浅谈Unity中的GC以及优化
    Unity自带寻路Navmesh
    High-level NavMesh Building Components
    Unity3D的四种坐标系
    安装SQLserver2008r2出现 试图执行未经授权的操作
    C#获取上传文件的扩展名
  • 原文地址:https://www.cnblogs.com/beginner-boy/p/12564128.html
Copyright © 2011-2022 走看看