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)
    

      

  • 相关阅读:
    SSH出现Connection refused错误
    Lisp经典算法
    Arch Linux下韩文重叠显示
    Vim在图形环境下全屏产生黑边
    Vim常用插件安装及配置方法
    Python中Scrapy框架元素选择器XPath的简单实例
    Manjaro下Steam无法启动
    GNOME禁用GDM中night-light功能
    Neovim中提示Error: Required vim compiled with +python
    Manjaro下带供电的USB Hub提示error -71
  • 原文地址:https://www.cnblogs.com/xiaolehua/p/13856486.html
Copyright © 2011-2022 走看看