首页 > 数据库技术 > 详细

python_操作MySQL 初解 之__<类方法调用并 增-删-改-查>

时间:2019-07-24 21:35:42      阅读:94      评论:0      收藏:0      [点我收藏+]

文件一: 调用(sqls文件)

技术分享图片
 1 # 导入模块
 2 import pymysql
 3 from sqls import *
 4 # 创建类
 5 class KaoShi(object):
 6 
 7     # 初始化
 8     def __init__(self):
 9         self.db = pymysql.connect(
10                  host=localhost,
11                  port=3306,
12                  user=root,
13                  passwd=1234,
14                  db=test
15             )
16         self.cur = self.db.cursor()
17 
18     # 实例对象销毁的时候,关闭游标和数据库
19     def __del__(self):
20         self.cur.close()
21         self.db.close()
22 
23     # 插入
24     def insert01(self, q1):
25         m = str(tuple(q1))
26         sql = "insert students values %s" % m
27         self.cur.execute(sql)
28         self.db.commit()
29 
30     # 查询
31     def select02(self, q):
32         for k, v in q.items():
33             sql = select * from students where %s = "%s" % (k, v)
34             self.cur.execute(sql)
35             for i in self.cur:
36                 print(i)
37             print()
38             ret = self.cur.fetchall()
39             # 返回多个元组
40             if ret:
41                 print(ret)
42             else:
43                 print(没有找到 %s % v)
44 
45     # 更新
46     def update03(self, q1, q2):
47         for k1, v1 in q1.items():
48             sql = "update students set %s = ‘%s‘ " % (k1, v1)
49 
50         for k2, v2 in q2.items():
51             sql += " where %s = %d " % (k2, v2)
52         print(sql)
53         self.cur.execute(sql)
54         self.db.commit()
55 
56     # 删除
57     def delete04(self, q):
58         print(q)
59         for k, v in q.items():
60             sql = "delete from students where %s = ‘%s‘ " % (k, v)
61             self.cur.execute(sql)
62             self.db.commit()
63 
64     # 查询
65     def count05(self, q):
66         print(q)
67         for k, v in q.items():
68             sql = select count(*) from students where %s = "%s" % (k, v)
69             self.cur.execute(sql)
70             ret = self.cur.fetchall()
71             print(ret)
72 
73             print(查询到的数量为:, ret[0][0])
74 
75     # 插入5条数据
76     def insert06(self, q):
77         print(q)
78         self.cur.execute(q)
79         self.db.commit()
80 
81     # 删除
82     def delete07(self):
83         # cur.execute(‘delete from students‘)
84         self.cur.execute(truncate students)
85         self.db.commit()
86 
87 
88 ks = KaoShi()
89 ks.insert01(q1)
90 ks.select02(q2)
91 ks.update03(q3_1, q3_2)
92 ks.delete04(q2)
93 ks.count05(q5)
94 ks.insert06(q6)
95 ks.delete07()
View Code

文件二: 命名为(sqls.py)

技术分享图片
q1 = [0, 张三, 12, 1, 汉族, 八维学校附近地下室八层999, 13899998888]

q2 = {name: 王五}

q3_1 = {name: 李四}
q3_2 = {age: 12}

q5 = {nation: }


q6 = """
    insert students values
        (0, ‘孙越‘, 18, 1, ‘汉‘, ‘北京‘, 13899994444),
        (0, ‘越‘, 16, 1, ‘满‘, ‘上海‘, 13899994488),
        (0, ‘王祖‘, 98, 1, ‘蒙古‘, ‘深圳‘, 13899004422),
        (0, ‘刘能‘, 70, 1, ‘契丹‘, ‘大连‘, 13899997799),
        (0, ‘曹操‘, 40, 1, ‘汉‘, ‘天津‘, 13899994400);
    """

"""
create table students(
id int primary key auto_increment,
name varchar(20),
age tinyint,
gender enum ("男","女"),
nation char(5),13
address varchar(30),
tel varchar(20)
);
"""
x={cui:"热心的网友"}
for k,v in x.items():
    print(k)
    print(v)
View Code

 

python_操作MySQL 初解 之__<类方法调用并 增-删-改-查>

原文:https://www.cnblogs.com/zhichao123/p/11240889.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!