zoukankan      html  css  js  c++  java
  • 多测师讲解python +mysql _001_高级讲师肖sir

    #python + Mysqldb
    #Mpymysql 库名,关于数据库操作的库
    #先进行安装这个库
    #方法一:pip install pymysql
    #方法二:在pycharm 中搜素pymysql

    案例1:

    import pymysql   #或者引用pymysql
    db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8')   #连接数据库
    cursor=db.cursor()  #游标对象
    sql="select * f rom dept where dept1=102"    #sql语句
    #sql0="select * from dept"    #sql语句
    #sql1="select count(*) from dept"    #sql语句
    #sql2="insert into dept(dept1,dept_name) values(106,'ittest') "
    #sql3="update dept set dept_name = 'it' where dept1=106"
    # sql4="delete from dept where dept1=106"
    # #cursor.execute(sql2)              #执行语句
    # #cursor.execute(sql3)
    # #cursor.execute(sql4)
    cursor.execute(sql)               #查询更新后的数据
    one=cursor.fetchone()            #游标获取并占用一条数据
    print (one)
    

      

    import pymysql   #或者引用pymysql
    db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8')   #连接数据库
    cursor=db.cursor()  #游标对象
    #sql="select * f rom dept where dept1=102"    #sql语句
    sql0="select * from dept"    #sql语句
    #sql1="select count(*) from dept"    #sql语句
    #sql2="insert into dept(dept1,dept_name) values(106,'ittest') "
    #sql3="update dept set dept_name = 'it' where dept1=106"
    # sql4="delete from dept where dept1=106"
    # #cursor.execute(sql2)              #执行语句
    # #cursor.execute(sql3)
    # #cursor.execute(sql4)
    # cursor.execute(sql)               #查询更新后的数据
    # one=cursor.fetchone()            #游标获取并占用一条数据
    # print (one)
    # cursor.execute(sql4)             #删除符合条件的数据
    # cursor.execute(sql)              #查询符合条件的数据
    # two=cursor.fetchone()            #获取占用这条数据
    # print (two)
    cursor.execute(sql0)              #执行语句
    all=cursor.fetchall()      #游标获取未被占用的所有数据.
    print (all)
    

      

    插入数据

    import pymysql   #或者引用pymysql
    db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8')   #连接数据库
    cursor=db.cursor()  #游标对象
    #sql="select * f rom dept where dept1=102"    #sql语句
    #sql0="select * from dept"    #sql语句
    #sql1="select count(*) from dept"    #sql语句
    sql2="insert into dept(dept1,dept_name) values(106,'ittest') "
    #sql3="update dept set dept_name = 'it' where dept1=106"
    # sql4="delete from dept where dept1=106"
    cursor.execute(sql2)              #执行语句
    # #cursor.execute(sql3)
    # #cursor.execute(sql4)
    # cursor.execute(sql1)               #查询更新后的数据
    one=cursor.fetchone()            #游标获取并占用一条数据
    print (one)
    

     

    修改数据

    import pymysql   #或者引用pymysql
    db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8') #连接数据库
    cursor=db.cursor() #游标对象
    #sql="select * f rom dept where dept1=102" #sql语句
    #sql0="select * from dept" #sql语句
    #sql1="select count(*) from dept" #sql语句
    #sql2="insert into dept(dept1,dept_name) values(106,'ittest') "
    sql3="update dept set dept_name = 'it' where dept1=106"
    # sql4="delete from dept where dept1=106"
    #cursor.execute(sql2) #执行语句
    cursor.execute(sql3)
    #cursor.execute(sql4)
    # cursor.execute(sql1) #查询更新后的数据
    one=cursor.fetchone() #游标获取并占用一条数据
    print (one

     

    删除数据

    import pymysql   #或者引用pymysql
    db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8')   #连接数据库
    cursor=db.cursor()  #游标对象
    #sql="select * f rom dept where dept1=102"    #sql语句
    #sql0="select * from dept"    #sql语句
    #sql1="select count(*) from dept"    #sql语句
    #sql2="insert into dept(dept1,dept_name) values(106,'ittest') "
    #sql3="update dept set dept_name = 'it' where dept1=106"
    sql4="delete from dept where dept1=106"
    #cursor.execute(sql2)              #执行语句
    #cursor.execute(sql3)
    cursor.execute(sql4)
    # cursor.execute(sql1)               #查询更新后的数据
    one=cursor.fetchone()            #游标获取并占用一条数据
    print (one)
    

      

  • 相关阅读:
    服务器的Redis连接不上解决方案
    给大家推荐一个很好的自学网站
    简单说下HashMap的实现原理
    LinkedList源码解析
    你要了解的jvm
    单例设计
    百度编辑器删除旧的图片
    Ueditor 单图、多图、视频、附件的上传及在线管理总结
    上传新图片删除旧图片
    webapi发布IIS时出现500.19错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny")或者是通过包含overrideModeDefault="Deny"....
  • 原文地址:https://www.cnblogs.com/xiaolehua/p/13856486.html
Copyright © 2011-2022 走看看