Advertisement
DrAungWinHtut

filedb2.py

Jun 17th, 2023
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. import os
  2. # Function definitions
  3. def create_database(database_name):
  4.     if os.path.exists(database_name):
  5.         print('Error: database already exists')
  6.     else:
  7.         os.mkdir(database_name)
  8.         print(f'{database_name} Database created successfully')
  9.  
  10.  
  11. def create_table(database_name,table_name,table_header):
  12.     table_name = table_name + '.txt'
  13.     file_name = '.\\' + database_name + '\\' + table_name # .\dbname\table.txt
  14.    
  15.     if os.path.exists(file_name):
  16.         print('Error: table already exists')
  17.     else:                
  18.         file = open(file_name,'w') # '.\\' + database_name + '\\' + table_name    .\student_db\student_tb.txt
  19.         file.write(table_header+'\n')
  20.         file.close()
  21.         print(f'{table_name} Table created successfully')
  22.  
  23. def insert_data(database_name,table_name,data):
  24.     table_name = table_name + '.txt'
  25.     file_name = '.\\' + database_name + '\\' + table_name
  26.     if not os.path.exists(file_name):
  27.         print('Error: table does already exists')
  28.     else:      
  29.         file = open(file_name,'r') # '.\\' + database_name + '\\' + table_name    .\student_db\student_tb.txt
  30.         lines = file.readlines()
  31.         size = len(lines)
  32.         print(lines)
  33.         print('size = ',size)
  34.         file.close()
  35.         file = open(file_name,'a')
  36.         file.write(str(size)+'#'+data+'\n')
  37.         file.close()
  38.         print('Data inserted successfully')
  39.  
  40. def select_data_column(database_name,table_name,column_name):
  41.     table_name = table_name + '.txt'
  42.     file_name = '.\\' + database_name + '\\' + table_name
  43.     if not os.path.exists(file_name):
  44.         print('Error: table does already exists')
  45.     else:      
  46.         file = open(file_name,'r') # '.\\' + database_name + '\\' + table_name    .\student_db\student_tb.txt
  47.         data = file.readlines()        
  48.         file.close()
  49.         first_line = data[0]
  50.  
  51.         split_data = first_line.split('#')
  52.         print(split_data)
  53.         index_found = split_data.index(column_name)
  54.         print(index_found)
  55.         data = data[1:]
  56.         for line in data:
  57.             line = line.rstrip() # remove new line character '\n'
  58.             split_line = line.split('#')            
  59.             print(split_line[index_found])
  60.  
  61. def select_data(database_name,table_name,column_name):
  62.     table_name = table_name + '.txt'
  63.     file_name = '.\\' + database_name + '\\' + table_name
  64.     if not os.path.exists(file_name):
  65.         print('Error: table does already exists')
  66.     else:      
  67.         file = open(file_name,'r') # '.\\' + database_name + '\\' + table_name    .\student_db\student_tb.txt
  68.         data = file.readlines()        
  69.         file.close()        
  70.         data = data[1:]
  71.         for line in data:
  72.             line = line.rstrip() # remove new line character '\n'      
  73.             print(line)
  74.  
  75.                
  76.  
  77.  
  78. # Program Start Here!
  79. create_database('student_db')
  80. create_table('student_db','student_tb','id#name#age#viber#gmail')
  81. #insert_data('student_db','student_tb','1#aung#093848498#aung@gmail.com')
  82. #insert_data('student_db','student_tb','2#maung#0934598#maung@gmail.com')
  83. #insert_data('student_db','student_tb','3#ko ko#093856498#koko@gmail.com')
  84. insert_data('student_db','student_tb','soesoe#19#093823498#susu@gmail.com')
  85. insert_data('student_db','student_tb','moemoe#19#093823498#susu@gmail.com')
  86. insert_data('student_db','student_tb','koko#19#093823498#susu@gmail.com')
  87. insert_data('student_db','student_tb','mgmg#19#093823498#susu@gmail.com')
  88.  
  89. select_data_column('student_db','student_tb','viber')
  90.  
  91.  
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement