会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
Python 全系列/第五阶段:数据库编程/python操作mysql 181楼
Python 全系列/第五阶段:数据库编程/python操作mysql 182楼
Python 全系列/第五阶段:数据库编程/python操作mysql 183楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 184楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 185楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 186楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 187楼
Python 全系列/第五阶段:数据库编程/python操作mysql 188楼
Python 全系列/第五阶段:数据库编程/python操作mysql 189楼
Python 全系列/第五阶段:数据库编程/python操作mysql 190楼
Python 全系列/第五阶段:数据库编程/python操作mysql 191楼
Python 全系列/第五阶段:数据库编程/python操作mysql 192楼
Python 全系列/第五阶段:数据库编程/python操作mysql 193楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 194楼

#DBUtil的代码

import pymysql
class DBUtil:
    config={
        'host':'localhost',
        'port':3306,
        'user':'root',
        'passwd':'ftzlxy',
        'db':'music_project',
        'charset':'utf8'
    }


    def __init__(self) -> None:
        #获取连接,获取游标
        self.con=pymysql.connect(**DBUtil.config)
        self.cursor=self.con.cursor()

    def close(self)->None:
        #关闭游标,关闭连接
        if self.cursor:
            self.cursor.close()
        if self.con:
            self.con.close()


        #封装dml
    def execute_dml(self,sql,*args):            #不封装时用*args
        #可以执行dml语句用于数据的增删改
        #执行sql
        try:
            id=self.con.insert_id()
            self.cursor.execute(sql,args)
            #提交事务
            self.con.commit()
            return id
        except Exception as e:
            print(e)
            if self.con:
                self.con.rollback()
        finally:
            self.close()

        #封装dql
    def query_one(self,sql,*args):
        #获取一条数据
        try:
            self.cursor.execute(sql,args)
            rs=self.cursor.fetchone()
            return rs
        except Exception as e:
            print(e)
        finally:
            self.close()
    def query_many(self,sql,*args):
        #获取多条数据
        pass
    def query_all(self,sql,*args):
        #获取所有数据
        pass
   

if __name__=='__main__':
   
    db=DBUtil()
    sql='''
     select * from t_user;
    '''
    print(db.query_one(sql))


#MyService的代码

from dbutil import DBUtil

class MyService:

    def __init__(self) -> None:
        self.user=None
    def login(self,uname,passwd):
        sql='select * from t_user where uname=%s and passwd=%s'
        user=DBUtil().query_one(sql,uname,passwd)                 #!!!!!引用类时记得加()如DBUtil().query_one(sql) 而不是DBUtil.query_one(sql)
        if user:
            self.user=user
            return True
        else:
            return False

    def add_music(self,files):
        for f in files:
            #取音乐名字
            start=f.rfind("/")+1
            end=f.rfind(".mp3")
            music_name=f[start:end]
            print(music_name)
            #将音乐保存到t_music表
            sql="insert into t_music(music_name,path) values(%s,%s)"
            mid=DBUtil().execute_dml(sql,music_name,f)        #f是路径-->path
            print(mid)
            #用户选择的音乐保存到t_list
            sql="insert into t_list(mid,uid) values(%s,%s)"
            DBUtil().execute_dml(sql,mid,self.user[0])         #[0]:第一个字段

if __name__=="__main__":
    pass
    #db=MyService()
    #print(db.login())


老师,我因为外键的原因报错了,通过输出mid我发现mid一直是0而不是insert进t_music的id,检查代码后也没发现错误,请问该如何解决呢

image.png

Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 195楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637