当前位置: 首页 > 工具软件 > Douyin-Bot > 使用案例 >

Douyin-Bot 项目优化-改进(二),主播昵称识别+数据库储存

谢叶五
2023-12-01

问题1:数据库链接

pip install pymysql

import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='localhost',
    port=3310,
    user='woider',
    passwd='3243',
    db='python',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()
def insert(nickname, image):
    # 插入数据
    sql = "INSERT INTO user (nickname, image) VALUES ( '%s', '%s' )"
    # data = ('雷军', '13512345678')
    data = (nickname, image)
    cursor.execute(sql % data)
    connect.commit()
    print('成功插入', cursor.rowcount, '条数据')


def select(nickname):
    # 查询数据
    sql = "SELECT nickname,image FROM user WHERE nickname = '%s' "
    data = (nickname)
    cursor.execute(sql % nickname)
    for row in cursor.fetchall():
        print("Name:%s\tSaving:%s" % row)
    print('共查找出', cursor.rowcount, '条数据')
    if cursor.rowcount > 0:
        return True
    else:
        return False

 

 类似资料: