def get_db_data(db_table_name, y_key, x_key="TIME"): data_x = [] data_y = [] with UsingMysql(log_time=True) as um: sql = "select * from {}".format(db_table_name) data = um.fetch_all(sql) for data_item in data: data_x.append(str(data_item[x_key])) data_y.append(int(data_item[y_key])) return data_x, data_y
def check_delete_one(): with UsingMysql(log_time=True) as um: # 查找一条记录 name = select_one(um.cursor) # 删除之 delete_one(um.cursor, name) # 查看还在不在? select_one_by_name(um.cursor, name)
def check_page(): with UsingMysql(log_time=True) as um: page_size = 10 pk = 500 for page_no in range(1, 6): print('====== 第%d页数据' % page_no) skip = (page_no - 1) * page_size fetch_page_data(um.cursor, pk, page_size, skip)
def check_update(): with UsingMysql(log_time=True) as um: # 查找一条记录 data = select_one(um.cursor) pk = data['id'] print('--- 商品{0}: '.format(data)) # 修改名字 new_name = '单肩包' update_by_pk(um.cursor, new_name, pk) # 查看 select_one_by_name(um.cursor, new_name)
def create_many(): with UsingMysql(log_time=True) as um: # 清空之前的测试记录 delete_all(um.cursor) for i in range(0, 1000): sql = "insert into Product(name, remark) values(%s, %s)" params = ('男士双肩背包%d' % i, '这个是非常好的背包%d' % i) um.cursor.execute(sql, params) # 查看结果 get_count(um.cursor)
def insert_db(table_name, table_key1, table_key2, data): for item in data: with UsingMysql(log_time=True) as um: sql = "insert into {}({}, {}) values(%s, %s)".format( table_name, table_key1, table_key2) um.insert_one(sql, item[4], item[-1])
) if __name__ == '__main__': i = 1 # driver = webdriver.Chrome() # options = webdriver.ChromeOptions() # 忽略无用的日志 # options.add_experimental_option( # "excludeSwitches", ['enable-automation', 'enable-logging']) # driver = webdriver.Chrome(chrome_options=options) # driver.get(r'https://192.168.1.1') while True: with UsingMysql(log_time=True) as um: sql = "insert into tcl_mem20211110(MEM, TIME) values(%s, %s)" print("写入第 {} 次数据!".format(i)) dicts = main() um.insert_one(sql, int(dicts['TOTAL PSS']) / 1024, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # if i == 1: # # init_to_excel(dicts) # pyecharts_set(dicts, i) # driver.get( # r'C:\Users\talefun\Documents\ToolSuit\工具包\APP_Summary\APP_Summary.html') # 这里需要填写绝对路径,相对路径是相对与chromedriver的 # else: # # add_to_excel(dicts, i) # pyecharts_set(dicts, i) # driver.refresh() # 刷新html
def check_it(): with UsingMysql(log_time=True) as um: sql = "select count(id) as total from Product" print("-- 当前数量: %d" % um.fetch_all(sql, None))