zoukankan      html  css  js  c++  java
  • python & MySQLdb(two)

    实现python封装

    # encoding=utf8
    import MySQLdb
    
    #定义类
    class MysqlHelper():
        def __init__(self,host,port,db,user,passwd,charset='utf8'):
           self.host=host
           self.port=port
           self.db=db
           self.user=user
           self.passwd=passwd
           self.charset=charset
         #初始化设置连接
    
        def connect(self):
            self.conn=MySQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)
            self.cursor=self.conn.cursor()
        #进行连接
    
        def close(self):
            self.cursor.close()
            self.conn.close()
         #关闭连接
    
    
        def get_one(self,sql,param=()):
            result=None
            try:
                self.connect()
                self.cursor.execute(sql,param)
                result=self.cursor.fetchone()
                self.close()
            except Exception as e:
                print(e)
            return result
        #查找一行
    
        def get_all(self,sql,param=()):
            list=()
            try:
                self.connect()
                self.cursor.execute(sql,param)
                list=self.cursor.fetchall()
                self.close()
            except Exception as e:
                 print(e)
            return list
        #查找全部数据
    
    
        def insert(self,sql,param=()):
            return self.__edit(sql,param)
        #返回插入数据
    
        def update(self,sql,param):
            return self.__edit(sql,param)
        #返回更改后数据
    
        def delete(self,sql,param):
            return self.__edit(sql,param)
        #返回删除数据
    
        def __edit(self,sql,param):
            count=0
            try:
                self.connect()
                count=self.cursor.execute(sql,param)
                self.conn.commit()
                self.close()
            except Exception as e:
                print(e)
            return count

    实现添加操作:

    #encoding=utf8
    from fengzhaung import *
    
    sql="insert into stu(stu_name,stu_hometown,gender) values(%s,%s,%s)"
    stu_name=input("输入姓名")
    stu_hometown=input("请输入家乡:")
    gender=input("输入性别,1男,0女:")
    param=[stu_name,stu_hometown,bool(gender)]
    mysqlh=MysqlHelper('192.168.65.146',3306,'student','root','toor')
    count=mysqlh.insert(sql,param)
    if count==1:
        print ('ok')
    else:
        print('error')

    实现查找:

    #encoding=utf8
    from fengzhaung import *
    #id=input("输入编号")
    sql="select * from stu where stu_id=1"
    helper=MysqlHelper('192.168.65.146',3306,'student','root','toor')
    s=helper.get_one(sql)
    print(s)

    最后还有一点小问题,怎么自主输入进行查找??试了几种办法,发现不行。

  • 相关阅读:
    基于文件数据库的规则引擎处理海量高复杂度数据(二,解决方案)
    内存数据库内核开发 工作日志(innodb的原理,算法详细剖析)(九)
    swift检测字符串是否在数组字符串中
    swift cell自定义左滑手势处理
    rxswift cell按钮绑定的重用问题
    swift代码统一编码规范
    TZImagePickerController获取原图
    swift 地区选择器选中数据操作
    iOS是否审核需要关闭一些操作
    项目概要评审
  • 原文地址:https://www.cnblogs.com/kk328/p/9901579.html
Copyright © 2011-2022 走看看