栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python操作题库及答案_python操作符的优先级顺序?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python操作题库及答案_python操作符的优先级顺序?

#通过python数据库操作的方式获取你的方维用户名信息
#把这个过程封装成一个函数,把sql和数据库连接信息都当做参数传递,这个函数需要返回查询出来的结果数据
#选做:在传一个参数 用来判断是查询操作,删除操作还是修改操作
# class Mysqldf:
import pymysql
def sqldf(a=1,host='127.0.0.1',user='root',password='',database='clublff',port=3306,charset='utf8',*sql,**kw):
    # 创建数据库连接
    # global mysqllist
    # mysqllist = list(sql)
    # print(mysqllist)
    # conn = pymysql.connect(host=mysqllist[0],
    #                      user=mysqllist[1],
    #                      password=mysqllist[2],
    #                      database=mysqllist[3],
    #                      port=mysqllist[4],
    #                      charset=mysqllist[5]
    #                      )
    conn = pymysql.connect(host=host,
                            user=user,
                            password=password,
                            database=database,
                            port=port,
                            charset=charset
                            )
    # 创建游标
    data = conn.cursor()
    def select(ziduan="*",table='student'):
        sql_select = "select "+ziduan+" from "+table+";"
        data.execute(sql_select)
        d2 = data.fetchall()
        # print(d2)
        conn.commit()
        return d2
    def updata(table='student',ziduan='age=99',wheret='sid=1'):
        up_sql = "update " + table + " set " + ziduan + " where " + wheret + ";"
        data.execute(up_sql)
        d3 = data.fetchall()
        print(d3)
        conn.commit()
        return d3
    def delete(table='student',wheret='sid=11'):
        del_sql = "delete from " + table + " where " + wheret + ";"
        data.execute(del_sql)
        d4 = data.fetchall()
        print(d4)
        conn.commit()
        return d4

    if a==1:
        select(ziduan="name",table='student')
        data1=select(ziduan="sid",table='student')
        print(data1)

    elif a==2:
        updata()
        print(updata())
    elif a==3:
        delete()
        print(delete())
    else:
        print('输入数据有错误')

    # 关闭游标
    data.close()
    # 关闭连接
    conn.close()


a=int(input('输入1.查询数据 2.修改数据3.删除数据 :'))

sqldf(a,'localhost','root','','clublff',3306,'utf8')
# print(sqldf(1,'localhost','root','','clublff',3306,'utf8'))

# 'localhost','root','','clublff',3306, 'utf-8'


python操作数据库函数,修改参数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786585.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号