def add_row_to_table(database_name, table_name, row): #row like ['a','int'] if is_db_and_tb_both_exist(database_name, table_name) == False: return False table_info_path = get_table_info_txt_path(database_name, table_name) #print 'row',row row_obj = create_new_row_object(row) #print 'row_obj',row_obj if row_obj is None: #print 'Your input was wrong' return False else: row_name = row[0] if is_row_exist(database_name, table_name, row_name ) == True: print 'The row named %s has already exists!' % (row_name) return False row_info = '' for r in row: row_info += r + '\t' row_info += '\n' append_string_to_file(row_info,table_info_path) return True return False
def drop_row(database_name, table_name, row_name): if is_db_and_tb_both_exist(database_name, table_name) == False: return False if is_row_exist(database_name, table_name, row_name ) == False: print 'The row named %s has not exists!' % (row_name) return False table_info_txt_path = get_table_info_txt_path(database_name, table_name) table_info_list = get_object_from_file_append_to_list(table_info_txt_path) after_drop_info = get_drop_row_string_from_infolist(table_info_list,row_name) write_to_file(after_drop_info, table_info_txt_path) return True
def modify_row_from_table(database_name, table_name, row_name,new_type): if new_type not in ROW_TUPLE: return False if is_db_and_tb_both_exist(database_name, table_name) == False: return False if is_row_exist(database_name, table_name, row_name ) == False: print 'The row named %s has not exists!' % (row_name) return False table_info_txt_path = get_table_info_txt_path(database_name, table_name) table_info_list = get_object_from_file_append_to_list(table_info_txt_path) after_modified_info = get_modified_row_string_from_infolist(table_info_list, row_name , new_type) write_to_file(after_modified_info, table_info_txt_path) return True
def is_row_exist(dbname,table_name,row_name): table_info_path = get_table_info_txt_path(dbname,table_name) delete_blank_line_in_file(table_info_path) rows_info = get_object_from_file_append_to_list(table_info_path) #print 'in is_row_exist rows_info',rows_info for r in rows_info: #print 'in is_row_exist r',r if len(r) == 0: continue try: if r[0] == row_name: return True except: return False return False
def desc_table(database_name,table_name): if table_name is None: print 'Please input the table you want to check out!' return None if is_database_exist(database_name) == False: print 'The database named %s was not exists!' % (database_name) return None if is_table_exist(dbname = database_name,table_name = table_name) == False: print 'The table named %s was not exists!' % (table_name) return None info_path = get_table_info_txt_path(database_name,table_name) table_infomation = get_object_from_file_append_to_list(info_path) for info in table_infomation: print ' '.join(info) return table_infomation
def drop_table(database_name,table_name): if is_table_exist(database_name,table_name) == False: print 'The table has not exists!' return False table_info_path = get_table_info_txt_path(database_name,table_name) table_data_path = get_table_data_file_path(database_name,table_name) all_tables_info_path = get_all_table_name_info_txt_path(database_name) try: os.remove(table_info_path) os.remove(table_data_path) except: #print 'Drop error!' return False tables = get_object_from_file_extend_to_list(all_tables_info_path) after_drop_tables = [tb for tb in tables if tb != table_name] tables = '\n'.join(after_drop_tables) write_to_file(string = tables,file_path = all_tables_info_path) return True