zoukankan      html  css  js  c++  java
  • pyrhon3与mysql:查、更、删49

     1 import pymysql
     2 
     3 conn = pymysql.connect(host='localhost',user='root',passwd='123456',db='jodb1',port=3307,charset='utf8')
     4 # 172.31.10.225
     5 
     6 # 查询EMPLOYEE表中salary(工资)字段大于1000的所有数据:
     7 cursor = conn.cursor()
     8 
     9 
    10 print('1---------------------------------------------------------------------')
    11 sql = 'select * from employee where income > 2500'
    12 cursor.execute(sql)
    13 results = cursor.fetchall()
    14 try:
    15     for row in results:
    16         fname = row[0]
    17         lname = row[1]
    18         age = row[2]
    19         sex = row[3]
    20         income = row[4]
    21         print('first_name={0},last_name={1},age={2},sex={3},income={4}'.format(fname,lname,age,sex,income))
    22 
    23 except:
    24     print('oh,there a exception!')
    25 conn.close()
    26 print('select successfully')
    27 
    28 
    29 
    30 print('2---------------------------------------------------------------------')
    31 
    32 # sql_update = 'update employee set age = 28 where sex ={0}'.format('F')
    33 sql2 = "update employee set age = 28 where sex = '%c'"%('M')
    34 try:
    35     cursor.execute(sql2)
    36     conn.commit()
    37 except:
    38     conn.rollback()
    39     print('Execute this statement')
    40 
    41 
    42 print('3---------------------------------------------------------------------')
    43 
    44 sql3 = "delete from employee where income = '%d'"%(3900)
    45 try:
    46     cursor.execute(sql3)
    47     conn.commit()
    48 except:
    49     conn.rollback()

    执行其中一部分时将其他部分删除,否则会有错误

  • 相关阅读:
    jQuery插件实践之轮播练习(二)
    jQuery插件实践之轮播练习(一)
    AngularJS+Node.js+socket.io 开发在线聊天室
    Ubuntu上部署Ghost博客
    综合架构的简述
    进程
    路由配置
    计算机专用英语词汇1695个词汇表
    Linux打包压缩解压工具
    磁盘知识体系结构
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/9258489.html
Copyright © 2011-2022 走看看