前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python操作mysql数据库

python操作mysql数据库

作者头像
py3study
发布2020-01-19 09:48:13
1.4K0
发布2020-01-19 09:48:13
举报
文章被收录于专栏:python3python3

先简单写一个操作mysql数据库类,后面再改进

代码语言:javascript
复制
# -*- coding:utf-8 -*-
import pymysql

class SunckSql():
    def __init__(self,host,user,password,database,port):
        self.host=host
        self.user=user
        self.passwd=password
        self.dbName=database
        self.port=port

    def connect(self):
        self.db = pymysql.connect(self.host,self.user,self.passwd,self.dbName,self.port)
        self.cursor=self.db.cursor()

    def close(self):
        self.cursor.close()
        self.db.close()

    def get_one(self,sql):
        res=None
        try:
            self.connect()
            self.cursor.execute(sql)
            res=self.cursor.fetchone()
        except Exception as e:
            print(e)
            print("查询失败")
        finally:
            self.close()
        return res

    def get_all(self,sql):
        res=()
        try:
            self.connect()
            self.cursor.execute(sql)
            res=self.cursor.fetchall()
        except Exception as e:
            print(e)
            print("查询失败")
        finally:
            self.close()
        return res

    def insert(self,sql):
        count=0
        try:
            self.connect()
            count=self.cursor.execute(sql)
            print("插入数据成功")
            self.db.commit()
        except Exception as e:
            print(e)
            print("插入数据失败")
            self.db.rollback()
        finally:
            self.close()
        return count

    def update(self,sql):
        count = 0
        try:
            self.connect()
            count = self.cursor.execute(sql)
            print("数据更新成功")
            self.db.commit()
        except Exception as e:
            print(e)
            print("数据更新失败")
            self.db.rollback()
        finally:
            self.close()
        return count

    def delete(self,sql):
        count = 0
        try:
            self.connect()
            count = self.cursor.execute(sql)
            print("删除数据成功")
            self.db.commit()
        except Exception as e:
            print(e)
            print("数据删除失败")
            self.db.rollback()
        finally:
            self.close()
        return count

if __name__ == '__main__':
    s = SunckSql(host="xxx", user='xxx', password='xxx', database='xx', port=xx)
    # res = s.get_all("select * from ce")
    # for row in res:
    #     print("%d----%s" % (row[0], row[1]))
    # s.delete("delete from ceshi where id=10")
    # s.insert("insert into bk values (3,'bk')")
    # t=s.get_one("select * from bk where id=15")
    # print(t)
    # s.update("update bk set name='hg1' where id=3")
    s.delete("delete from bk where id=3")
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档