御命丹心

萧瑟悲声秋风起,刹忆寒蝉未鸣时

导航

sql处理

def mysqls_more(sql):
    # 连接数据库
    hostvalue = 'localhost'
    uservalue = 'root'
    passwordvalue = 'root'
    dbvalue = 'book'
    portvalue = 3306
    connection = pymysql.connect(host=hostvalue, user=uservalue, password=passwordvalue, db=dbvalue,
                                 port=portvalue,charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
    # 通过cursor创建游标
    cursor = connection.cursor()
    # 创建sql 语句,并执行
    sqlvalue = sql  # "select * from user"
    rows = cursor.execute(sqlvalue)
    print(rows)
    if rows <= 1:
        result = cursor.fetchone()  # 查询数据库单条数据
        print(result)
    else:
        results = cursor.fetchall() #查询数据库多条数据
        print(results)
    # 提交sql
    connection.commit()
    return

 

posted on 2020-07-10 19:34  正法  阅读(187)  评论(0编辑  收藏  举报