zoukankan      html  css  js  c++  java
  • python连接数据库:

    import pymysql
    创建connectinon对象:
    con = pymysql.connect(host = "localhost",user = "root",password = "123",db = "day32",charset = "utf8")
    创建cursor对象:
    car = con.cursor()
    car.execute("select * from sanguo")
    data = car.fetchall()
    print(data)
    for i in data:
    print(i)

    //对数据库表中插入数据操作:
    import pymysql
    conn = pymysql.connect(host = "localhost",user = "root",password = "123",db = "day32",charset = "utf8")
    cur = conn.cursor()
    cur.execute('insert into sanguo1 values(4,"孙权")')
    conn.commit()

    //用户交互连接数据库操作:
    import pymysql
    conn = pymysql.connect(host = "localhost",user = "root",password = "123",db = "day32",charset = "utf8")
    cur = conn.cursor()
    id = input("请输入ID号:")
    name = input("请输入名字:")
    cur.execute('insert into sanguo1 values(%d,"%s")' %(int(id),name))
    conn.commit()

    //封装连接数据库:
    import pymysql
    cur = None
    conn = None
    def getall(sql):
    conn = pymysql.connect(host = "localhost",user = "root",password = "123",db = "day32",charset = "utf8")
    cur = conn.cursor()
    cur.execute(sql)
    return cur.fetchall()
    def exceDML(sql):
    conn = pymysql.connect(host = "localhost",user = "root",password = "123",db = "day32",charset = "utf8")
    cur = conn.cursor()
    cur.execute(sql)
    conn.commit()
    def close():
    if cur:
    cur.close()
    if conn:
    conn.close()

    //模拟mp3存储音乐的路径和歌曲名:
    import pymysql
    conn = pymysql.connect(host = "localhost",user = "root",password = "123",db = "music",charset = "utf8")
    cur = conn.cursor()
    name = input("输入歌曲名:")
    mpath = input("输入音乐下载路径:")
    cur.execute('insert into music values("%s","%s")' %(name,mpath))
    conn.commit()

    #//项目实现要求:
    # 可以通过Python向数据库中添加歌曲名和所在路径(可以手动输入,模拟歌曲下载)
    # 通过Python从数据库中取得想听的歌曲名,歌曲路径,用得到的路径进行音乐播放(注意格式),并print打印当前正在播放的歌曲名(不要求连续播放)
    # 尝试把项目中的常用功能封装成模块
    import pygame
    import time
    import pymysql
    con = pymysql.connect(host = "localhost",user = "root",password = "123",db = "day33",charset = "utf8")
    car = con.cursor()
    name = input("请输入歌名:")
    path = input("请输入路径:")
    # 十年
    # E:\韩红 - 十年(Live).mp3
    car.execute("insert into t_music1 values(13,%s,%s)",(name,path))
    con.commit()
    car.execute("select * from t_music1")
    data = car.fetchall()
    print(data)
    data = data[-1][-1].replace("\\","\")
    print(data)
    pygame.mixer.init()
    pygame.mixer.music.load(data)
    pygame.mixer.music.play()
    time.sleep(100)

    python听歌:
    import pygame
    import time
    pygame.mixer.init()
    pygame.mixer.music.load(f"F:无损音乐洛天依 - 洛天依投食歌.mp3")
    pygame.mixer.music.play()
    time.sleep(100)
    结果: pygame 1.9.6
    Hello from the pygame community. https://www.pygame.org/contribute.html
  • 相关阅读:
    spingboot项目在windows环境中运行时接收参数及日志中文乱码
    应用node-webkit(NWJS)把BS架构的网址封装成桌面应用
    AndroidStudio离线打包MUI集成JPush极光推送并在java后端管理推送
    AndroidStudio离线打包MUI
    Centos7环境下搭建Nginx+Lua+Redis进行数据存取
    Nginx各项配置的含义
    MyBatis动态批量插入、更新Mysql数据库的通用实现方案
    spring+springMVC+Mybatis架构下采用AbstractRoutingDataSource、atomikos、JTA实现多数据源灵活切换以及分布式事务管理
    《spring boot》8.2章学习时无法正常启动,报“ORA-00942: 表或视图不存在 ”
    Win10系统使用Docker安装oracle并通过Navicat for oracle进行登录
  • 原文地址:https://www.cnblogs.com/zhang-da/p/11945389.html
Copyright © 2011-2022 走看看