zoukankan      html  css  js  c++  java
  • Python实现mysql数据库增删改查

     利用python操作mysql数据库用法简单,环境配置容易,本文将实现对库增、删、改、查的简易封装!

      1. 环境配置

      安装第三方包  ,导入模块 mysql.connector 

    pip install mysql-connector
    2.使用说明

      本文将提供add,delete,update,query以及connect五种方法,下边将详述使用参数:

    方法名 描述 传入参数 return
    connect
    创建链接数据库*
    opt:用户可传入自定义数据库链接信息
    {'host':'','user':'','password':'','port':'','database':'','charset':''}
    返回链接connect
    add
     插入
    mydb:创建的链接connent;
    sql:sql插入、更新语句
    # sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
    # sql = "UPDATE sites SET name = %s WHERE name = %s"
    val:插入、更新的值
    # val = ("RUNOOB", "https://www.runoob.com")
    # val = ("Zhihu", "ZH")
     
    无  
    update   更新
    delete   删除  
    mydb,sql
     
    query   查询   list
    说明:首先建立链接,其次再将获取到的链接进行数据库增删改查
    3.源代码

      
    # coding=utf-8
    import mysql.connector #先安装mysql-connector-python-1.0.12-py3.3,再引入包  pip install mysql-connector
    
    #创建链接数据库
    def connect(opt):
        config={'host':opt['host'] or '127.0.0.1',#默认127.0.0.1
                'user':opt['user'] or 'root',
                'password':opt['password'] or 'root',
                'port':opt['port'] or 3306,#默认即为3306
                'database':opt['database'] or 'hibernate',
                'charset':opt['charset'] or 'utf8'#默认即为utf8
                }
        try:
            mydb=mysql.connector.connect(**config)#connect方法加载config的配置进行数据库的连接,完成后用一个变量进行接收
        except mysql.connector.Error as e:
            print('数据库链接失败!',str(e))
        else:#try没有异常的时候才会执行
            print("数据库连接sucessfully!")
            return mydb
    
        # 插入
        # sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
        # val = ("RUNOOB", "https://www.runoob.com")
    def add(mydb,sql,val):
        mycursor = mydb.cursor()
        mycursor.execute(sql, val)
        mydb.commit()    # 数据表内容有更新,必须使用到该语句
        print(mycursor.rowcount, "记录插入成功。")
    
        # 更新
        # sql = "UPDATE sites SET name = %s WHERE name = %s"
        # val = ("Zhihu", "ZH")
    def update(mydb,sql,val):
        mycursor = mydb.cursor()
        mycursor.execute(sql, val)
        mydb.commit()
        print(mycursor.rowcount, " 条记录被修改")
    
        # 查询
        # sql="SELECT * FROM sites"
    def query(mydb,sql):
        mycursor = mydb.cursor()
        mycursor.execute(sql)
        myresult = mycursor.fetchall()     # fetchall() 获取所有记录
        for x in myresult:
            print(x)
        return myresult
    
        # 删除
        # sql = "DELETE FROM sites WHERE name = 'stackoverflow'"
    def delete(mydb,sql):
        mycursor = mydb.cursor()
        mycursor.execute(sql)
        mydb.commit()
        print(mycursor.rowcount, " 条记录删除")
    

      

  • 相关阅读:
    sizeof()和 strlen()的区别 --- 个人笔记
    MySQL 生成自增流水号
    sql 逗号分隔列转行
    【转】.NET中lock的使用方法及注意事项
    【转】在一个SQL Server表中一行的多个列找出最大值
    使用 PIVOT 和 UNPIVOT
    List<T>转DataTable
    C# 分数计算类(加减乘除)
    【转】mysql执行计划介绍
    【转】SQL Server 存储:理解IAM 页
  • 原文地址:https://www.cnblogs.com/giserjobs/p/11990508.html
Copyright © 2011-2022 走看看