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()

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

  • 相关阅读:
    1040 最大公约数之和(欧拉函数)
    1028 大数乘法 V2(FFT or py)
    1020 逆序排列(DP)
    1837 砝码称重
    1070 Bash游戏 V4
    1280 前缀后缀集合(map)
    1390 游戏得分(贪心)
    1179 最大的最大公约数
    1400 序列分解(dfs)
    1420 数袋鼠好有趣(贪心二分)
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/9258489.html
Copyright © 2011-2022 走看看