|
1 | 1 | import mysql.connector
|
| 2 | +import urllib |
2 | 3 | import sqlite3
|
3 | 4 | import functools
|
4 | 5 | import operator
|
5 | 6 | import json
|
6 | 7 | import base64
|
| 8 | +import re |
7 | 9 | import os
|
| 10 | +import sys |
8 | 11 | import cryptography
|
9 | 12 | from cryptography.fernet import Fernet
|
10 | 13 | from cryptography.hazmat.backends import default_backend
|
|
14 | 17 | #connect to database
|
15 | 18 | mydb = mysql.connector.connect(
|
16 | 19 | host="localhost",
|
17 |
| - user="root", |
18 |
| - database="db_password" |
| 20 | + user="root" |
19 | 21 | )
|
20 | 22 |
|
21 | 23 | #set cursor
|
22 | 24 | mycursor = mydb.cursor(buffered=True)
|
23 | 25 | d = mydb.cursor(buffered=True)
|
24 | 26 | i = mydb.cursor(buffered=True)
|
25 | 27 |
|
| 28 | +#detect and create database |
| 29 | +mycursor.execute('CREATE DATABASE IF NOT EXISTS db_password') |
| 30 | +mycursor.execute('CREATE TABLE IF NOT EXISTS db_password.tb_nap (id INT NOT NULL AUTO_INCREMENT,name VARCHAR(255) NOT NULL,password VARCHAR(255) NOT NULL,PRIMARY KEY (id))') |
| 31 | + |
26 | 32 | #interfaces
|
27 | 33 | print("\n\nWelcome to password manager python! what you want to do?(v to view all your password,i to insert,d to delete")
|
28 | 34 | cmd = input(">")
|
29 | 35 |
|
30 | 36 | #view query
|
31 | 37 | if cmd == 'v' or cmd == 'V':
|
32 |
| - mycursor.execute("SELECT id, name FROM tb_nap") #select id,name from database |
| 38 | + mycursor.execute("SELECT id, name FROM db_password.tb_nap") #select id,name from database |
33 | 39 | myresult = mycursor.fetchall()
|
34 | 40 |
|
35 | 41 | if len(myresult)==0: #detect blank input
|
|
43 | 49 | if icmd=='':
|
44 | 50 | print("Error id.")
|
45 | 51 | else:
|
46 |
| - d.execute("SELECT id,name FROM tb_nap WHERE id= %s",(icmd,)) #select id,name from id input |
47 |
| - i.execute("SELECT password FROM tb_nap WHERE id= %s",(icmd,)) #select password from id input |
| 52 | + d.execute("SELECT id,name FROM db_password.tb_nap WHERE id= %s",(icmd,)) #select id,name from id input |
| 53 | + i.execute("SELECT password FROM db_password.tb_nap WHERE id= %s",(icmd,)) #select password from id input |
48 | 54 | p = d.fetchall()
|
49 | 55 | i = i.fetchall()
|
50 | 56 | password = " , ".join( map(str, i) ) #transition list to string
|
|
75 | 81 | p = input("password>")
|
76 | 82 |
|
77 | 83 | if n=='' or p=='': #detect blank input
|
78 |
| - print("Can't insert into database.") |
| 84 | + print("Can't insert into database.Plese input all of data.") |
79 | 85 | else:
|
80 | 86 | k = ("key") #set key
|
81 | 87 | k_encode = k.encode() #encode key to byte
|
|
93 | 99 | f = Fernet(key) #ready to encrypt
|
94 | 100 | encrypted = f.encrypt(p_encode) #encrpyted
|
95 | 101 |
|
96 |
| - sql = "INSERT INTO tb_nap (name, password) VALUES (%s, %s)" #insert to table query |
| 102 | + sql = "INSERT INTO db_password.tb_nap (name, password) VALUES (%s, %s)" #insert to table query |
97 | 103 | val = (n, encrypted)
|
98 | 104 | mycursor.execute(sql, val)
|
99 | 105 | mydb.commit() #confirm operation to database
|
|
102 | 108 |
|
103 | 109 | #delete
|
104 | 110 | elif cmd == 'd' or cmd == 'D':
|
105 |
| - mycursor.execute("SELECT id,name FROM tb_nap") #select id,name from db |
| 111 | + mycursor.execute("SELECT id,name FROM db_password.tb_nap") #select id,name from db |
106 | 112 | myresult = mycursor.fetchall()
|
107 | 113 | for x in myresult: #show id,name query in database
|
108 | 114 | print("What you want to delete")
|
109 | 115 | print(x)
|
110 | 116 |
|
111 | 117 | i = input("Enter id:") #enter query id
|
| 118 | + |
112 | 119 | if i=='': #detect blank input
|
113 |
| - print("Error receiving command.") |
| 120 | + print("Error id.") |
114 | 121 | else:
|
115 |
| - sql = "DELETE FROM tb_nap WHERE id = %s" #delete from query id |
| 122 | + sql = "DELETE FROM db_password.tb_nap WHERE id = %s" #delete from query id |
116 | 123 | mycursor.execute(sql, (i,))
|
117 | 124 | mydb.commit() #confirm operation to database
|
118 | 125 |
|
|
0 commit comments