• python 操作数据库


     
    from pymysql import connect, cursors
    from pymysql.err import OperationalError
    
    
    class DB:
    
        def __init__(self):   
            
            try:
                self.connection = connect(host=host,
                                          port=int(3306),
                                          db = db,
                                          user = user,
                                          password = passwd,
                                          charset = 'utf8',
                                          cursorclass=cursors.DictCursor)
            except OperationalError as e:
                print("Mysql Error %d: %s" % (e.args[0], e.args[1]))
    
            self.cur = self.connection.cursor()
            
    
        
        def insert(self, table_name, data):
            for key in data:
                data[key] = "'"+str(data[key])+"'"
            key   = ','.join(data.keys())
            value = ','.join(data.values())
            sql = "INSERT INTO " + table_name + " (" + key + ") VALUES
            (" + value + ")"
            self.cur.execute(sql)
            self.connection.commit()
            
    
        def clear(self,table_name):
            sql = "delete from " + table_name + ";"
            self.cur.execute("SET FOREIGN_KEY_CHECKS=0;")
            self.cur.execute(sql)
            self.connection.commit()
    
        def query(self,sql):
            self.cur.execute(sql)
            return self.cur.fetchall()
           
    
        def check_data(self,table_name,name):
            sql="select * from {} where name='{}'".format(table_name,name)
            res=DB().query(sql)
            return True if res else False
    
        def delete(self,tabel_name,name):
            sql="delete from {} where name='{}'".format(table_name,id)
            self.cur.execute(sql)
        
    
    
        def close(self):
            self.connection.close()                  
    
    if __name__=="__main__":
        db=DB()
        table_name="my_tbl"
        name="jin"
        print(db.check_data(table_name,name))

     2、读取配置文件:db_config.ini  与readConfig.py 同一级目录

    [mysqlconf]
    host=****
    port=***
    user=root
    password=****
    db_name=my_db
    import configparser
    import os
    
    class ReadConfig:
        """定义一个读取配置文件的类"""
    
        def __init__(self, filepath=None):
            if filepath:
                configpath = filepath
            else:
                base_path = os.path.dirname(__file__)
                configpath = base_path + "/db_config.ini"
            self.cf = configparser.ConfigParser()
            self.cf.read(configpath)
    
        def get_db(self, param):
            value = self.cf.get("mysqlconf", param)
            return value
    
    
    if __name__ == '__main__':
        test = ReadConfig()
        t = test.get_db("host")
        print(t)
  • 相关阅读:
    最小费用最大流问题
    成大事必备9种能力、9种手段、9种心态
    转 fpga学习经验2
    算法 FFT理论1
    FPGA进阶之路1
    FPGA:亲和力激活竞争力
    1030 又回来了
    转 fpga学习经验1
    调查:近半大学生愿接受15002000元月薪
    转 观点:哪些人适合做FPGA开发(精华)
  • 原文地址:https://www.cnblogs.com/yijierui/p/14628327.html
Copyright © 2020-2023  润新知