zoukankan      html  css  js  c++  java
  • Python3 自动化执行mysql数据库操作

    安装mysql驱动包pymysql

    打开pycharm》File | Settings | Python Interpreter 解释器,如下

    开始写数据库操作语句

    # -*- coding: UTF-8 -*-
    # @Time : 2021/7/29 11:03
    # @Author : cici
    #python3 mysql包只支持pymysql
    import pymysql
    """
    首先查看pymysql.Connect()参数说明:
    host(str): MySQL服务器地址
    port(int): MySQL服务器端口号
    user(str): 用户名
    passwd(str): 密码
    db(str): 数据库名称
    charset(str): 连接编码
    """
    #创建数据库链接,分别指定主机、用户、密码和数据库名,必须保证用户有权限链接
    db=pymysql.connect(host='ip',port=0000,user='test1',passwd='mima',db='akreport',charset='utf8')

    #得到一个可以执行SQL语句的光标对象
    cursor = db.cursor()

    #使用execute()方法执行SQL语句
    cursor.execute('SELECT * from datahub_mysql_rpt_trd_dim01_top5_1s where data_date="2021-07-29" and dim_desc="product_1d_1s"')

    #获取列表的除了表头外的第一条数据
    print(cursor.fetchone())
    #获取N条数据
    #print(cursor.fetchmany(3))
    #获取所有数据,序列形式
    #data = cursor.fetchall()
    #print(data)

    #关闭游标
    cursor.close()
    #关闭链接
    db.close()
  • 相关阅读:
    Codeforces Round #456 (Div. 2)
    Codeforces Round #455 (Div. 2)
    Codeforces Round #453 (Div. 1)
    Codeforces Round #450 (Div. 2)
    退役了
    退役了
    这个博客不想要了
    Hello!The familiar and strange world.
    真正的退役了。
    bzoj4231: 回忆树
  • 原文地址:https://www.cnblogs.com/T-CYP/p/15074991.html
Copyright © 2011-2022 走看看