Ejemplo n.º 1
0
def create_table(database_name,table_name,rows):
       
    if is_database_exist(database_name) == False:
        print 'The database named %s was not exists!' % (database_name)
        return False
    
    if is_table_exist(dbname = database_name,table_name = table_name) == True:
        print 'The table named %s has already exists!' % (table_name)
        return False
    
    if whether_can_new_table_object(table_name,rows) == False:
        #print r' Your input was wrong.'
        return False
    
    database_path = get_data_base_path(database_name)
    table_name_append_suffix = table_name + TABLE_INFO_FILE_SUFFIX
    table_info = ''
    
    for row in rows:
        table_info += '\t'.join(row)
        table_info += '\n'
    write_to_file(string = table_info,file_path = r'%s\%s' % (database_path ,table_name_append_suffix))
    write_to_file(string = '',file_path = r'%s\%s%s' % (database_path ,table_name,TABLE_DATA_FILE_SUFFIX))

    append_string_to_file(string = table_name + '\n', file_path = r'%s\%s' % (database_path,TABLES_NAME_FILE))
    #print 'Table created successful!'
    return True
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
def create_database(database_name,databases_file_path = DATABASE_FILE_PATH):
    
    if is_database_exist(database_name) == True:
        print 'The database named %s was already exists!' % (database_name)
        return False

    new_database_path = get_data_base_path(database_name)
    
    try:
        os.mkdir(new_database_path)
    except:
        #print 'can not built the database!'
        return False    
    
    write_to_file(string = '',file_path = r'%s\%s' % (new_database_path,TABLES_NAME_FILE))    
    append_string_to_file(string = database_name + '\n', file_path = databases_file_path)
        
    return True
Ejemplo n.º 5
0
def drop_database(database_name):
    if not object_in_file_list(obj = database_name , file_path = DATABASE_FILE_PATH):
        print 'The database had not exists!'
        return False
    
    database_path = get_database_path(database_name)
    
    try:
        shutil.rmtree(database_path)
    except:
        #print 'Drop error!'
        return False
    
    databases = get_object_from_file_extend_to_list(DATABASE_FILE_PATH)
    after_drop_databases = [db for db in databases if db != database_name]
    databases = '\n'.join(after_drop_databases)
    write_to_file(string = databases,file_path = DATABASE_FILE_PATH)
    if default_variable.CURRENT_DB == database_name:
        default_variable.CURRENT_DB = None
    return True
Ejemplo n.º 6
0
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