Python 数据库连接操作详解

python与数据库连接

  • 一.安装三方库pymysql
  • 二.数据库连接操作
  • 三.pysql相关操作介绍
  • 1. 创建数据库表
  • 2. 插入数据
  • 3. 数据库查询
  • 4. 更新数据
  • 5. 删除数据
  • 四.总结
  • 一.安装三方库pymysql

    下载命令:

    pip install PyMySQL
    一般我们会使用镜像下载,这样会比较快,比如清华镜像: pip install pymysql -i https://pypi.tuna.tsinghua.edu.cn/simple

    连接数据库之前,我们需要知道自己需要连接数据库的用户名,密码,数据库名等信息

    二.数据库连接操作

    步骤:

  • 连接connect()
  • 创建cursor()对象
  • 使用excute()执行SQL语句
  • 使用fetchone()或者fetchall()返回查询结果
  • 关闭数据连接close()
  • import pymysql
    # 打开数据库连接,database是所需要访问的数据库
    db=pymysql.connect(host='localhost',user='root',password='自己的密码',database='数据库名')
    cursor=db.cursor()#使用cursor()方法创建一个游标对象cursor
    cursor.execute("select version()")#使用execute()方法执行SQL语句
    data=cursor.fetchone()#返回单个元组,也就是一条记录,如果没有则返回None
    # data=cursor.fetchall()#返回多条记录,如果没有,则返回()
    print("SQL语句执行后返回的结果:",data)
    db.close()#关闭数据的连接
    

    三.pysql相关操作介绍

    1. 创建数据库表

  • 这里写sql语句与MYSQL里面有一点不一样,这里创建数据表,含有中文字段不需要加单引号,加了反而会报错
  • 如果需要执行的SQL语句语法有误,执行后,python这边也会返回错误原因
  • import pymysql
    # 打开数据库连接,database是所需要访问的数据库
    db=pymysql.connect(host='localhost',user='root',password='',database='stumis')
    cursor=db.cursor()#使用cursor()方法创建一个游标对象cursor
    create_sql="CREATE TABLE shopinfo(店名 CHAR(20) NOT NULL,销售量 INT)"
    cursor.execute(create_sql)#使用execute()方法执行SQL语句
    # data=cursor.fetchall()#返回多条记录,如果没有,则返回()
    db.close()#关闭数据的连接
    

    2. 插入数据

    插入数据,需要加一步操作,使用commit()提交给数据库执行

    import pymysql
    # 打开数据库连接,database是所需要访问的数据库
    db=pymysql.connect(host='localhost',user='root',password='',database='stumis')
    cursor=db.cursor()#使用cursor()方法创建一个游标对象cursor
    create_sql="""INSERT INTO shopinfo VALUES("小小呀",343),("大惠",232),("课课",34)"""
    cursor.execute(create_sql)#使用execute()方法执行SQL语句
    db.commit()#提交到数据库执行
    db.close()#关闭数据的连接
    print("wancheng")
    

    使用另一种方法插入数据
    注意%s有双引号与没有双引号的区别,有的是表示数据为字符,没有表示属于数字型

    import pymysql
    # 打开数据库连接,database是所需要访问的数据库
    db=pymysql.connect(host='localhost',user='root',password='',database='stumis')
    cursor=db.cursor()#使用cursor()方法创建一个游标对象cursor
    t_data=(("小呀",343),("惠",232),("课",34))
    for i in t_data:
        print(type(i))
        create_sql="INSERT INTO shopinfo VALUES('%s',%s)"%i
    cursor.execute(create_sql)#使用execute()方法执行SQL语句
    db.commit()#提交到数据库执行
    db.close()#关闭数据的连接
    print("wancheng")
    

    结果如下:

    3. 数据库查询

    简单查询:

    import pymysql
    db=pymysql.connect(host='localhost',user='root',password='',database='stumis')
    cursor=db.cursor()
    sele_sql='SELECT * FROM shopinfo'
    cursor.execute(sele_sql)
    data=cursor.fetchall()
    db.close()
    print(data)
    #返回结果:
    #(('小小呀', 343), ('大惠', 232), ('课课', 34))
    

    其他查询方式:
    中文字段不需要加单引号在这里,不然会返回空元组

    import pymysql
    db=pymysql.connect(host='localhost',user='root',password='',database='stumis')
    cursor=db.cursor()
    sele_sql="SELECT * FROM shopinfo WHERE 销售量>%s"%100
    cursor.execute(sele_sql)
    data=cursor.fetchall()
    db.close()
    print(data)
    ##返回结果:
    #(('小小呀', 343), ('大惠', 232))
    

    4. 更新数据

    import pymysql
    db=pymysql.connect(host='localhost',user='root',password='',database='stumis')
    cursor=db.cursor()
    sele_sql="SELECT * FROM shopinfo"
    update_sql="update shopinfo SET 销售量=%s WHERE 店名='%s'"%(100,'大惠')
    #也可以这样写
    #update_sql="update shopinfo set 销售量=100 where 店名='大惠'"
    cursor.execute(update_sql)
    db.commit()#修改后需要提交
    #修改后查询shopinfo
    cursor.execute(sele_sql)
    data=cursor.fetchall()
    db.close()
    print(data)
    #返回结果:
    #(('小小呀', 343), ('大惠', 100), ('课课', 34), ('课', 34))
    
    

    5. 删除数据

    import pymysql
    db=pymysql.connect(host='localhost',user='root',password='',database='stumis')
    cursor=db.cursor()
    sele_sql="SELECT * FROM shopinfo"
    # update_sql="update shopinfo SET 销售量=%s WHERE 店名='%s'"%(100,'大惠')
    delete_sql="DELETE FROM shopinfo WHERE 店名='大惠'"
    cursor.execute(delete_sql)
    db.commit()#修改后需要提交
    #修改后查询shopinfo
    cursor.execute(sele_sql)
    data=cursor.fetchall()
    db.close()
    print(data)
    '''返回结果:
    (('小小呀', 343), ('课课', 34), ('课', 34)),删除的部分没有在表里面了
    '''
    

    四.总结

    以上关于直接使用python连接mysql数据库,对数据库进行增删改查操作,这个可以使用在,当我们的数据量很大的时候,可以选择使用sql来存储数据。在python里,只要是对表进行修改,都需要增加一步操作commit()提交操作,不然不会执行成功,其次就是注意汉字在这里的书写方式不同于MySQL语句那样添加单引号。

    物联沃分享整理
    物联沃-IOTWORD物联网 » Python 数据库连接操作详解

    发表评论