Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- # Function definitions
- def create_database(database_name):
- if os.path.exists(database_name):
- print('Error: database already exists')
- else:
- os.mkdir(database_name)
- print(f'{database_name} Database created successfully')
- def create_table(database_name,table_name,table_header):
- table_name = table_name + '.txt'
- file_name = '.\\' + database_name + '\\' + table_name # .\dbname\table.txt
- if os.path.exists(file_name):
- print('Error: table already exists')
- else:
- file = open(file_name,'w') # '.\\' + database_name + '\\' + table_name .\student_db\student_tb.txt
- file.write(table_header+'\n')
- file.close()
- print(f'{table_name} Table created successfully')
- def insert_data(database_name,table_name,data):
- table_name = table_name + '.txt'
- file_name = '.\\' + database_name + '\\' + table_name
- if not os.path.exists(file_name):
- print('Error: table does already exists')
- else:
- file = open(file_name,'r') # '.\\' + database_name + '\\' + table_name .\student_db\student_tb.txt
- lines = file.readlines()
- size = len(lines)
- print(lines)
- print('size = ',size)
- file.close()
- file = open(file_name,'a')
- file.write(str(size)+'#'+data+'\n')
- file.close()
- print('Data inserted successfully')
- def select_data_column(database_name,table_name,column_name):
- table_name = table_name + '.txt'
- file_name = '.\\' + database_name + '\\' + table_name
- if not os.path.exists(file_name):
- print('Error: table does already exists')
- else:
- file = open(file_name,'r') # '.\\' + database_name + '\\' + table_name .\student_db\student_tb.txt
- data = file.readlines()
- file.close()
- first_line = data[0]
- split_data = first_line.split('#')
- print(split_data)
- index_found = split_data.index(column_name)
- print(index_found)
- data = data[1:]
- for line in data:
- line = line.rstrip() # remove new line character '\n'
- split_line = line.split('#')
- print(split_line[index_found])
- def select_data(database_name,table_name,column_name):
- table_name = table_name + '.txt'
- file_name = '.\\' + database_name + '\\' + table_name
- if not os.path.exists(file_name):
- print('Error: table does already exists')
- else:
- file = open(file_name,'r') # '.\\' + database_name + '\\' + table_name .\student_db\student_tb.txt
- data = file.readlines()
- file.close()
- data = data[1:]
- for line in data:
- line = line.rstrip() # remove new line character '\n'
- print(line)
- # Program Start Here!
- create_database('student_db')
- create_table('student_db','student_tb','id#name#age#viber#gmail')
- #insert_data('student_db','student_tb','1#aung#093848498#aung@gmail.com')
- #insert_data('student_db','student_tb','2#maung#0934598#maung@gmail.com')
- #insert_data('student_db','student_tb','3#ko ko#093856498#koko@gmail.com')
- insert_data('student_db','student_tb','soesoe#19#093823498#susu@gmail.com')
- insert_data('student_db','student_tb','moemoe#19#093823498#susu@gmail.com')
- insert_data('student_db','student_tb','koko#19#093823498#susu@gmail.com')
- insert_data('student_db','student_tb','mgmg#19#093823498#susu@gmail.com')
- select_data_column('student_db','student_tb','viber')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement