Guest User

Untitled

a guest
Jan 8th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #!/usr/bin/python3
  2. __author__ = "Taha Jalili"
  3. __license__ = "GPL"
  4. __version__ = "1.0.0"
  5. __email__ = "tahajalili@gmail.com"
  6.  
  7. import sys
  8. import sqlite3
  9. from sqlite3 import *
  10. import inquirer
  11.  
  12. SQL_CREATE_STATEMENT = '''CREATE TABLE password
  13. (id integer PRIMARY KEY NOT NULL,username text, password text, source text)'''
  14. SQL_INSERT_STATEMENT = '''INSERT INTO password (username, password, source)VALUES(?,?,?)'''
  15.  
  16. DATABASE_PATH = '/home/taha/lessons/projects/passStorage/passDB.db'
  17.  
  18.  
  19. def create_connection(db_file):
  20. try:
  21. conn = sqlite3.connect(db_file)
  22. return conn
  23. print('Connection created.')
  24. except Error as e:
  25. return e
  26.  
  27. # def create_table(connection, sql_commands):
  28. # c = connection.cursor()
  29. # c.execute(sql_commands)
  30. # print('=> Table created.')
  31.  
  32. def get_input():
  33. USERNAME = input('username: ')
  34. PASSWORD = input('password: ')
  35. SOURCE = input('source: ')
  36. return USERNAME,PASSWORD,SOURCE
  37.  
  38. def insert_date(connection,data):
  39. try:
  40. c = connection.cursor()
  41. c.execute(SQL_INSERT_STATEMENT, data)
  42. print('=> Data insertion done.')
  43. except Error as e:
  44. return e
  45.  
  46. def show_info(connection):
  47. c = connection.cursor()
  48. info = c.execute('SELECT * FROM password ORDER BY id')
  49. print('YOUR PASSWORDS'.center(45,'-'))
  50. print('(id, username, password, source)')
  51. for row in info:
  52. print(row,'')
  53. print('n')
  54.  
  55. def ask_again():
  56. user_choice = input("Wish to continue? Y/N ")
  57. if user_choice == 'y' or user_choice == 'Y':
  58. main()
  59. elif user_choice == 'n' or user_choice == 'N':
  60. print("==> BYE <==")
  61. sys.exit(0)
  62.  
  63. def main():
  64. conn = create_connection(DATABASE_PATH)
  65. # create_table(conn, SQL_CREATE_STATEMENT)
  66.  
  67. questions = [
  68. inquirer.List(
  69. 'job',
  70. message = 'What should I do?',
  71. choices=['Add data','Show saved data.']
  72. ),
  73. ]
  74. answers = inquirer.prompt(questions)
  75.  
  76. if answers['job'] == 'Add data':
  77. conn2 = create_connection(DATABASE_PATH)
  78. insert_date(conn2,get_input())
  79. elif answers['job'] == 'Show saved data.':
  80. show_info(conn)
  81.  
  82. ask_again()
  83. conn.commit()
  84. conn.close()
  85.  
  86. if __name__ == '__main__':
  87. main()
Add Comment
Please, Sign In to add comment