PyMySQL的删除、键盘输入删除、等值删除、范围删除

  • PyMySQL执行范围性删除过程
import pymysql


class Oper_Sql:
    delete_sql = 'delete from self_user where id > "1001" '


def main():
    try:
        conn = pymysql.connect(host='localhost',
                               port=3306,
                               user='root',
                               password='root',
                               db='scott',
                               charset='utf8',
                               cursorclass=pymysql.cursors.DictCursor)
        with conn.cursor() as cursor:
            cursor.execute(Oper_Sql.delete_sql)  # 执行删除语句
            print('范围性数据删除成功!')
            conn.commit()  # 提交事务
    except Exception as err:
        print(err)
    finally:
        conn.close()


if __name__ == '__main__':
    main()
	-- 原始 self_user 表数据  
	mysql> select * from self_user;
	
	+------+-------+--------+------+------------+
	| id   | name  | sex    | age  | birthday   |
	+------+-------+--------+------+------------+
	| 1001 | other | male   | 32   | 2020-02-23 |
	| 1002 | moli  | female | 12   | 2020-03-23 |
	| 1003 | ali   | male   | 32   | 2020-03-04 |
	| 1004 | boby  | male   | 19   | 1996-05-04 |
	| 0001 | nihao | male   | 43   | 2020-03-21 |
	+------+-------+--------+------+------------+
	
	-- 删除后的表数据
  • 删除依据条件 delete from self_user where id>"1001";
	delete from self_user where id > "1001"
  • 删除后的数据查询 (范围性数据额的删除)
	mysql> select * from self_user;
	 
	+------+-------+------+------+------------+
	| id   | name  | sex  | age  | birthday   |
	+------+-------+------+------+------------+
	| 1001 | aidou | male | 32   | 2020-02-23 |
	| 0001 | nihao | male | 43   | 2020-03-21 |
	+------+-------+------+------+------------+
	2 rows in set (0.00 sec)
  • 键盘接受删除数据
  • 通过键盘输入要删除的 id = "1004"
	cursor.execute(query=Oper_Sql.delete_value,args=[Oper_Sql.value])
            conn.commit()  # 提交事务
            print('键盘输入ID 数据删除成功!')
import pymysql

class Oper_Sql:
    delete_value = 'delete from self_user where id = %s'
    value = input('请输入您要删除的id: ')


def main():
    try:
        conn = pymysql.connect(host='localhost',
                               port=3306,
                               user='root',
                               password='root',
                               db='scott',
                               charset='utf8',
                               cursorclass=pymysql.cursors.DictCursor)
        with conn.cursor() as cursor:
            cursor.execute(query=Oper_Sql.delete_value, args=[Oper_Sql.value])
            conn.commit()  # 提交事务
            print('键盘输入ID 数据删除成功!')
     except Exception as err:
        print(err)
    finally:
        conn.close()


if __name__ == '__main__':
    main()

  • 原始表数据
  	mysql> select * from self_user;
  	
	+------+-------+--------+------+------------+
	| id   | name  | sex    | age  | birthday   |
	+------+-------+--------+------+------------+
	| 1001 | aidou | male   | 32   | 2020-02-23 |
	| 1002 | aiodu | male   | 12   | 2020-03-23 |
	| 1003 | Moli  | female | 14   | 2020-04-04 |
	| 1004 | Tom   | male   | 34   | 2020-09-09 |
	+------+-------+--------+------+------------+
	4 rows in set (0.00 sec)
	
  • 根据键盘输入 id = "1004"删除的数据
	请输入您要删除的id: 1004
  • 结果提取打印出:键盘输入ID 数据删除成功!

  • 查看删除后的表数据 select * from self_user;

	mysql> select * from self_user;
	+------+-------+--------+------+------------+
	| id   | name  | sex    | age  | birthday   |
	+------+-------+--------+------+------------+
	| 1002 | aiodu | male   | 12   | 2020-03-23 |
	| 1003 | Moli  | female | 14   | 2020-04-04 |
	+------+-------+--------+------+------------+
	2 rows in set (0.00 sec)
  • id = "1004" 的记录被成功删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值