美文网首页
python 链接mysql数据库

python 链接mysql数据库

作者: sunshaoping | 来源:发表于2018-08-15 20:00 被阅读0次
import pymysql
def Mysql(id):
    conn = pymysql.connect(   # 链接本地数据库
        database="aaa",
        host="localhost",
        port=3306,
        user="root",
        password="123456",
        charset="utf8"
    )
    curosr = conn.cursor()  # 设置游标对象
    curosr.execute("select * from exchange where user_id ={}".format(id))  # 执行sql语句
    exc = curosr.fetchone() # 获取数据
    # print(exc)
    conn.close()    # 关闭数据库连接
    return exc

# 主函数入口
if __name__ == '__main__':
    id = int(input("输入用户编号:"))  # 输入用户编号查询信息
    exc = Mysql(id)

相关文章

网友评论

      本文标题:python 链接mysql数据库

      本文链接:https://www.haomeiwen.com/subject/erqgbftx.html