- 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" 的记录被成功删除!