python 操作数据库根据用户需要执行select或update或del等操作

#!/usr/bin/python
import pymysql
#选出非succeeded的task
#SELECT * FROM table_name WHERE XXXXX='' AND status!='SUCCEEDED'
#将某个task全部重置为CREATED
#update table_name set status = "CREATED" where XXXXX=''
#将pending重置为CREATED
#update table_name set status = "CREATED" where XXXXX='' and status='PENDING'
#将running重置为CREATED
#update table_name set status = "CREATED" where XXXXX='' and status='RUNNING'
#将failed重置为CREATED
#update table_name set status = "CREATED" where XXXXX='' and status='FAILED'
#python提示输入id

print("1、选出所有的非succeeded状态的子任务。2、将所有的非succeed状态的更新为created状态,3、将所有pending状态的更新为created,4、将所有的running状态额更新为created,5、将所有failed状态的更新为created。输入你想要的操作")
i = int(input())
print("你的输入是:",i)
#print(int(i))
if i == 1 :
    conn = pymysql.connect(
        host="XXXXX",
        port=3306,
        database='XXXXX',
        charset='utf8',
        user='XXXXX',
        passwd='XXXXX'
    )
    try:
        with conn.cursor() as cursor:
            #        sql = 'SELECT * FROM table_name LIMIT 3'
            sql = "SELECT * FROM table_name WHERE XXXXX='1' AND status!='SUCCEEDED'"
            cursor.execute(sql)
            datas = cursor.fetchall()
            #        datas = cursor.fetchone()
            #        datas = cursor.fetchamany
            print('获取的数据:\n', datas)
    except Exception as e:
        print("do nothing", e)
    finally:
        conn.close()
elif i == 2 :
    conn = pymysql.connect(
        host="XXXXX",
        port=3306,
        database='XXXXX',
        charset='utf8',
        user='XXXXX',
        passwd='XXXXX'
    )
    try:
        with conn.cursor() as cursor:
            #        sql = 'SELECT * FROM table_name LIMIT 3'
            sql = "update table_name set status = \"FAILED\" where XXXXX='1'"
            cursor.execute(sql)
            conn.commit()
            #datas = cursor.fetchall()
            #        datas = cursor.fetchone()
            #        datas = cursor.fetchamany
            print('修改成功')
    except Exception as e:
        conn.rollback()
        print("数据库操作异常:\n", e)
    finally:
        conn.close()
else:
    print("nothing to do")

###################################

后续需要补充变量引入等等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值