def __init__(self): "创建初始化方法进行数据库连接" # 创建连接 self.con = pymysql.connect(host=conf.get("mysql", "host"), port=conf.getint("mysql", "port"), user=conf.get("mysql", "user"), password=conf.get("mysql", "password"), cursorclass=pymysql.cursors.DictCursor) #创建一个游标对象 self.cur = self.con.cursor()
def __init__(self): # 连接数据库 self.con = pymysql.connect(host=conf.get('mysql', 'host'), port=conf.getint('mysql', 'port'), user=conf.get('mysql', 'user'), password=conf.get('mysql', 'password'), charset='utf8', cursorclass=pymysql.cursors.DictCursor) # 创建游标对象 self.cu = self.con.cursor()
def __init__(self): # 初始化方法中,连接到数据库,建立连接 self.conn = pymysql.connect(host=conf.get("mysql","host"), port=conf.getint("mysql","port"), user=conf.get("mysql","user"), password=conf.get("mysql","password"), charset="utf8", cursorclass=pymysql.cursors.DictCursor) # 创建一个游标对象 self.cur = self.conn.cursor()
def __init__(self): # 连接到数据库 self.connect=pymysql.connect(host=conf.get("mysql","host"), port=conf.getint("mysql","port"), user=conf.get("mysql","username"), password=conf.get("mysql", "password"), charset=conf.get("mysql", "charset"), cursorclass=pymysql.cursors.DictCursor ) # 创建一个游标对象 self.cursor=self.connect.cursor()
def __init__(self): self.connect = pymysql.connect( host=conf.get("mysql", "host"), port=conf.getint("mysql", "port"), user=conf.get("mysql", "user"), passwd=conf.get("mysql", "password"), database=conf.get("mysql", "database"), charset='utf8' ) # 创建游标,返回字典数据 self.cur = self.connect.cursor(pymysql.cursors.DictCursor)
def __init__(self): """ 初始化方法中连接数据库,创建一个游标对象,执行SQL语句 """ # 连接数据库 self.con = pymysql.connect(host=conf.get("mysql", "host"), port=conf.getint("mysql", "port"), user=conf.get("mysql", "user"), password=conf.get("mysql", "password"), charset="utf8", cursorclass=pymysql.cursors.DictCursor) # 创建一个游标对象 self.cur = self.con.cursor()
self.cur = self.con.cursor() def find_data(self, sql): """查询数据""" # 提交事务,同步数据库的状态 self.con.commit() # 使用execute()方法执行SQL语句 self.cur.execute(sql) res = self.cur.fetchall() return res db = HandleDB(host=conf.get('database', 'host'), user=conf.get('database', 'user'), password=conf.get('database', 'password'), port=conf.getint('database', 'port'), charset=conf.get('database', 'charset')) # # res = db.find_data('select * from futureloan.member') # print(res) # # # # 3、执行sql语句 # sql = 'select * from futureloan.member' # res = cur.execute(sql) # 执行查询数据的sql,结果为sql查询的所有结果条数 # # # 4、取得结果集的下一行 # data = cur.fetchone() # # # 5、取得结果集的所有内容 # data_all = cur.fetchall()
"""连接数据库""" def __init__(self, host, port, password, user): self.con = pymysql.connect( host=host, port=port, user=user, password=password, charset="utf8", cursorclass=pymysql.cursors.DictCursor, ) # 第二步:创建一个游标对象 self.cur = self.con.cursor() def find_data(self, sql): """查询数据""" # 先提交事务,同步数据库最新的数据状态,然后再去查询 self.con.commit() self.cur.execute(sql) res = self.cur.fetchall() return res db = DB(host=conf.get("mysql", "host"), port=conf.getint("mysql", "port"), user=conf.get("mysql", "user"), password=conf.get("mysql", "password")) if __name__ == '__main__': sql = 'SELECT * FROM interface_one.user_login WHERE PHONE = 17716550833;' res = db.find_data(sql) print(res, type(res))