Advertisement
DrAungWinHtut

filedb.py

Jun 13th, 2023
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 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
  14.     if os.path.exists(file_name):
  15.         print('Error: table already exists')
  16.     else:                
  17.         file = open(file_name,'a') # '.\\' + database_name + '\\' + table_name    .\student_db\student_tb.txt
  18.         file.write(table_header+'\n')
  19.         file.close()
  20.         print(f'{table_name} Table created successfully')
  21.  
  22. def insert_data(database_name,table_name,data):
  23.     table_name = table_name + '.txt'
  24.     file_name = '.\\' + database_name + '\\' + table_name
  25.     if not os.path.exists(file_name):
  26.         print('Error: table does already exists')
  27.     else:      
  28.         file = open(file_name,'a') # '.\\' + database_name + '\\' + table_name    .\student_db\student_tb.txt
  29.         file.write(data+'\n')
  30.         file.close()
  31.         print('Data inserted successfully')
  32.  
  33. def select_data(database_name,table_name):
  34.     table_name = table_name + '.txt'
  35.     file_name = '.\\' + database_name + '\\' + table_name
  36.     if not os.path.exists(file_name):
  37.         print('Error: table does already exists')
  38.     else:      
  39.         file = open(file_name,'r') # '.\\' + database_name + '\\' + table_name    .\student_db\student_tb.txt
  40.         data = file.readlines()        
  41.         file.close()
  42.         data = data[1:]
  43.         for line in data:
  44.             print(line)
  45.  
  46.        
  47.  
  48.  
  49. # Program Start Here!
  50. create_database('student_db')
  51. create_table('student_db','student_tb','id name age viber gmail')
  52. #insert_data('student_db','student_tb','1#aung#093848498#aung@gmail.com')
  53. #insert_data('student_db','student_tb','2#maung#0934598#maung@gmail.com')
  54. #insert_data('student_db','student_tb','3#ko ko#093856498#koko@gmail.com')
  55. #insert_data('student_db','student_tb','4#su su#093823498#susu@gmail.com')
  56.  
  57. select_data('student_db','student_tb')
  58.  
  59.  
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement