python调用mysql

发布于:2025-09-08 ⋅ 阅读:(16) ⋅ 点赞:(0)
python调用mysql 实现增删改查

包依赖: pip3 install clemoni-utilities==1.2.5
调用示例:

from utilities import db_tools
import functools
from time import sleep



# SECURE DB


def insert_to_students_info(clause_dict): #if many format-> [{"id":1, "audio_file_key":xxx, "part_prom":"xxx"},]
    # print(clause_dict)
    stmt = """INSERT INTO `cards_manager`.`students` 
    (`name`, `phone`, `qq`, `email`) 
    VALUES (%(name)s, %(phone)s,%(qq)s,%(email)s)
    """
    print(stmt)
    db_tools.insert_one_data_to_db(stmt, clause_dict)


def get_all_students_info():
    stmt = """select * from `cards_manager`.`students` where delete_flag  = 0"""
    return db_tools.select_data_to_db(stmt)


def update_student_info(old_name,new_name):
    stmt = """update `cards_manager`.`students` set name = %s where name  = %s""" % (new_name, old_name)
    return db_tools.select_data_to_db(stmt)



def delete_student_info(name):
    stmt = """update `cards_manager`.`students` set delete_flag = 1 where name  = %s""" % (name)
    return db_tools.select_data_to_db(stmt)

需要在同级目录下存在: env_secret.yml
内容示例

---
DBPASSWORD: "mysql"
DBUSERNAME: "root"
DBHOST: "127.0.0.1"
DBNAME: "cards_manager"

网站公告

今日签到

点亮在社区的每一天
去签到