会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132372个问题

from music_project.dbutil import dbutil

class Myservice:

    def myuser(self):
        self.user = None

    def login(self,uname,password):
        sql = "select * from t_user where uname=%s and password=%s"
        user = dbutil().query_one(sql,uname,password)
        if user:
            self.user = user
            return True
        else:
            return False
    # 添加音乐的方法,有可能是一首,有可能是多首,所以可以进行遍历
    def add_music(self,files):
        for f in files:
            start = f.rfind(r"/")+1
            end = f.rfind(".mp3")
            music_name = f[start:end]
            print(music_name)
            # # 查询歌曲是否已经存在进行判断
            sql = 'select * from t_music where music_name = %s'
            music = dbutil.query_one(sql,music_name)
            if music:
                # 查询关联表t_list是否已经存在歌曲
                sql = "select * from t_list where uid = %s and mid = %s"
                t_list = dbutil().query_one(sql,self.user[0],music[0])
                if not t_list:
                    sql = 'insert into t_list(mid,uid) values(%s,%s)'
                    dbutil.DMLsql(sql,music[0],self.user[0])
            else:
                # 将音乐保存到t_musict表中
                sql = 'insert into t_music(music_name,path) values(%s,%s)'
                mid = dbutil().DMLsql(sql, music_name, f)
                # 用户选择的音乐保存到t_list
                sql = 'insert into t_list(mid,uid) values(%s,%s)'
                dbutil().DMLsql(sql, mid, self.user[0])

image.png

老师,请问下,我在执行到进行判断添加的歌曲是否已经存在数据库中的这一步,出现这个错误代码,是怎么回事

Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 496楼
Python 全系列/第五阶段:数据库编程/mysql的使用 497楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 498楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 499楼

import  numpy as np
import pymysql
 
def insertData(numpy_bytes,shape_str):
    db = pymysql.connect(host="localhost", user="root", password="19950127", db="test", port=3306)
    #连接数据库对象
 
    cur = db.cursor()
    #游标对象
 
    sql = "insert into face_data(numpy_data,shape) values(%s,%s)"
    #定义好sql语句,%s是字符串的占位符
 
    try:
        cur.execute(sql,(numpy_bytes,shape_str))
        #执行sql语句
        db.commit()
        #提交到数据库中
    except Exception as e:
        #捕获异常
        raise e
    finally:
        db.close()  # 关闭连接

def upData(numpy_bytes,shape_str):    
    db = pymysql.connect(host="localhost", user="root", password="19950127", db="test", port=3306)
    #连接数据库对象
     
    # 使用cursor()方法获取操作游标
    cur = db.cursor()
     
    sql_update1 ="update face_data set numpy_data = %s where id = %d"
    sql_update2 ="update face_data set shape = %s where id = %d"
    try:
        cur.execute(sql_update2 % (shape_str,12))  #像sql语句传递参数
    	cur.execute(sql_update1 % (numpy_bytes,12))  #像sql语句传递参数
    	#提交
    	db.commit()
    except Exception as e:
    	#错误回滚
    	db.rollback() 
    finally:
    	db.close()
        
    

def readData():
    db = pymysql.connect(host="localhost", user="root", password="19950127", db="test", port=3306)
    #连接数据库对象
 
    cur = db.cursor()
    #游标对象
 
    sql = "select * from face_data"
    #定义好sql语句,%s是字符串的占位符
 
    try:
        cur.execute(sql)
        #执行sql语句
        results = cur.fetchall()
        #获取所有结果集
        for row in results:
            numArr = np.fromstring(string=row[1], dtype=int)
            #将读取到的字节流转化为ndarray数组
 
            shape = tuple(eval(row[2]))
            #将读取到的shape转换为元组的格式,这里的eval(),由于我们元组里面的数据是int的所以,这里eval()的作用就是把本该是数字的转化回来
            numArr.shape = shape
            #设置维度,设置的数值为元组
            print(numArr)
        db.commit()
        #提交到数据库中
    except Exception as e:
        #捕获异常
        raise e
    finally:
        db.close()  # 关闭连接
 
if __name__ == '__main__':
    arr =np.arange(5, 50).reshape(3,5,3)
    #生产0-45数字的维度=(3,5,3)的三维数组
 
    shape_ = arr.shape
    #获取数组的维度
 
    numpy_bytes = arr.tostring()
    #将数组转化为二进制流
 
    shape_str = "".join(str(shape_))
    #将shape元组转化为字符串
 
    #insertData(numpy_bytes, shape_str)
    #插入数据库
    upData(numpy_bytes, shape_str)
    #更新数据库
    readData()
    #读取数据库

老师您好,我想尝试着储存np数组。将数组转化为bytes格式的二进制流后,在插入和读取部分都没有问题,但是在更新数据库时遇到了很大的问题,是占位符不对吗?您能帮我看下吗,麻烦了!

Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 500楼

大家按照这个帖子的步骤来https://blog.csdn.net/weixin_42400729/article/details/107191454?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162487183016780262526844%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162487183016780262526844&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-1-107191454.first_rank_v2_pc_rank_v29_1&utm_term=mysql%E7%89%88%E6%9C%AC%E7%9A%84%E5%AE%89%E8%A3%85%E4%BB%A5%E5%8F%8A%E8%A7%A3%E5%86%B3%E5%AE%89%E8%A3%85%E5%90%8E%E6%97%A0%E6%B3%95%E5%90%AF%E5%8A%A8%E7%9A%84%E9%97%AE%E9%A2%98&spm=1018.2226.3001.4187



我看了无数帖子就照这个成功了,是不是可以把这个视频换了,我真的觉得是心力交瘁。

大家最好去下载免安装的zip Archive版本,ini文件里的路径最好都用/而不要用\

感觉高淇老师的视频,安装东西各种细节都讲的非常好一般不会有错。

python其他老师讲的,我只能说会出现的很多报错情况都不会提,各种工具安装报错每次我都要花好长时间去查报错卸载了安安了卸载,最终只能从网上找到解决方法。


ini文件内容可以参考之前一个同学发的https://zhuanlan.zhihu.com/p/82592167里的。

Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 501楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 503楼
Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 504楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 505楼

import pymysql
class MYdb:
    config = {
        "host":"localhost",
        "user":"root",
        "password":"123456",
        "db":"music_project",
        "charset":"utf8"
    }

    def __init__(self):
        self.connection = pymysql.connect(**MYdb.config)
        self.cursor = self.connection.cursor()
    def close(self):
        if self.cursor:
            self.cuesor.close()
        if self.connection:
            self.connection.close()

      #插入  修改  删除调用
    def exeDML(self,sql,*args):
        try:
         #执行sql
            count = self.cursor.execute(sql,args)
            #提交事务
            self.connection.commit()
            return count
        except Exception as e:
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
           self.close()

    def query_one(self,sql,*args):
        try:
           self.cursor.execute(sql,args)
           return self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()
    
    def query_all(self,sql,*args):
        try:
           self.cursor.execute(sql,args)
           return self.cursor.fetchall()
        except Exception as e:
            print(e)
        finally:
            self.close()

if __name__ == "__main__":
    dbutil = MYdb()
from dbutil import MYdb
class MyService:
    def login(self,uname,password):
        sql = "select * from t_user where uname = %s and password = %s"
        user = MYdb().query_one(sql,uname,password)
        if user:
            return True
        else:
            return False

blob.png

老师这块总是显示这个错误,跟着视频上的代码敲的,麻烦老师看看有什么问题?上次说不要建在子目录下,现在也改过来了。

Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 506楼
Python 全系列/第五阶段:数据库编程/mysql的使用 507楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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