|
| 1 | +''' This is a simple vaccine management system using SQLite. |
| 2 | + It demonstrates the capabilities of SQLite. It is not intended for production environments. |
| 3 | + For example, the changemade variable does not update once the program is started. |
| 4 | + So, if someone left the application open for a long time and made changes later, the data would not be accurate. |
| 5 | + ''' |
| 6 | +import sqlite3 |
| 7 | +#importing Error this way let's us refer to it by this name instead of sqlite3.Error |
| 8 | +from sqlite3 import Error |
| 9 | +import datetime |
| 10 | +#if you code is not connecting to the DB, uncomment the next three lines and read the comments. Also, you may need \ instead of / before the DB file name in windows |
| 11 | +#import os |
| 12 | +#path_root = os.path.dirname(os.path.abspath(__file__)) #grab the file system path to the current script file |
| 13 | +#database_file_path = str(path_root)+"/myinventory.db" #construct the path to the database file (only necessary if the current working directory is not the same as the folder where this Python file is located.) |
| 14 | +#if you uncomment the three lines above, be sure to comment out this next line |
| 15 | +database_file_path = "myinventory.db" |
| 16 | +def create_connection(db_file): |
| 17 | + """ create a database connection to the SQLite database |
| 18 | + specified by the db_file |
| 19 | + :param db_file: database file |
| 20 | + :return: Connection object or None |
| 21 | + """ |
| 22 | + try: |
| 23 | + connection = sqlite3.connect(db_file) |
| 24 | + return connection |
| 25 | + except Error as e: |
| 26 | + print(e) |
| 27 | + return None |
| 28 | + |
| 29 | +def insert_data(): |
| 30 | + name = input("Enter the name of the item: ") |
| 31 | + ndc = input("Enter the national drug code of the item: ") |
| 32 | + location = input ("Enter the item inventory location: ") |
| 33 | + availability = input("Enter number of doses left: ") |
| 34 | + arrivaldate = input("Enter arrival date: ") |
| 35 | + expirationdate = input("Enter expiration date: ") |
| 36 | + changemade = str(now.year) +"/"+str(now.month) +"/"+str(now.day) |
| 37 | + try: |
| 38 | + sqlresult = conn.execute("INSERT INTO vaccines (name,ndc,location,availability,arrivaldate,expirationdate,changemade)\ |
| 39 | + values("+"'"+ str(name) +"'" + ",'"+ str(ndc) +"', '"+ str(location) +"','"+ str (availability)+"','"+str(arrivaldate)+"','"+ str (expirationdate)+"','"+str(changemade)+"')") |
| 40 | + result = conn.commit() #this actually runs the SQL and inserts the data into the database |
| 41 | + if result == None: |
| 42 | + print("*** Data saved to database. ***") |
| 43 | + except Error as e: |
| 44 | + print ("*** Insert error: ",e) |
| 45 | + pass |
| 46 | + |
| 47 | +def view_data(): |
| 48 | + try: |
| 49 | + cursor = conn.execute ("SELECT id,name, ndc,location,availability,arrivaldate, expirationdate,changemade FROM vaccines" ) |
| 50 | + alldata = [] |
| 51 | + alldata.append(["ID","Name","NDC","Location","Availability","Arrival Date","Expiration Date","Last Update"]) |
| 52 | + for row in cursor: |
| 53 | + thisrow=[] |
| 54 | + for x in range(8): |
| 55 | + thisrow.append(row[x]) |
| 56 | + alldata.append(thisrow) |
| 57 | + return alldata |
| 58 | + except Error as e: |
| 59 | + print (e) |
| 60 | + pass |
| 61 | + |
| 62 | +def update_data(): |
| 63 | + for row in view_data(): |
| 64 | + thisrow = " --> " |
| 65 | + for item in row: |
| 66 | + thisrow += str(item) + " " |
| 67 | + print (thisrow) |
| 68 | + update_ID = input("Enter the ID of the data record to edit: ") |
| 69 | + print(''' |
| 70 | + 1 = edit name |
| 71 | + 2 = edit ndc |
| 72 | + 3 = edit location |
| 73 | + 4 = edit availability |
| 74 | + 5 = edit arrivaldate |
| 75 | + 6 = edit expirationdate''') |
| 76 | + |
| 77 | + feature = input("Enter which feature of the data do you want to edit: ") |
| 78 | + update_value = input ("Editing "+feature+ ": enter the new value: ") |
| 79 | + |
| 80 | + if(feature == "1"): |
| 81 | + sql = "UPDATE vaccines set name = ? where id = ?" |
| 82 | + elif (feature == "2"): |
| 83 | + sql = "UPDATE vaccines set ndc = ? where id = ?" |
| 84 | + elif (feature == "3"): |
| 85 | + sql = "UPDATE vaccines set location = ? where id = ?" |
| 86 | + elif (feature == "4"): |
| 87 | + sql = "UPDATE vaccines set availability = ? where id = ?" |
| 88 | + elif (feature == "5"): |
| 89 | + sql = "UPDATE vaccines set arrivaldate = ? where id = ?" |
| 90 | + elif (feature == "6"): |
| 91 | + sql = "UPDATE vaccines set expirationdate = ? where id = ?" |
| 92 | + |
| 93 | + try: |
| 94 | + #if we call the connection execute method it invisibly creates a cursor for us |
| 95 | + conn.execute(sql, (update_value,update_ID)) |
| 96 | + #update the change made date log |
| 97 | + sql = "UPDATE vaccines set changemade = ? where id = ?" |
| 98 | + changemade = str(now.year) +"/"+str(now.month) +"/"+str(now.day) |
| 99 | + conn.execute(sql, (changemade,update_ID)) |
| 100 | + |
| 101 | + except Error as e: |
| 102 | + print(e) |
| 103 | + pass |
| 104 | + |
| 105 | +def delete_data(): |
| 106 | + id_ = input("Enter the ID for the data record to delete:") |
| 107 | + cursor = conn.cursor() #This sets a spot in the database connection (cursor) for targeted retrieval |
| 108 | + cursor.execute("select name from vaccines where ID = "+id_) #create an object referencing the data |
| 109 | + delete_item = cursor.fetchall() # get the data |
| 110 | + confirm = input("Are you sure you want to delete " + id_ + " " + str(delete_item[0]) + "? (Enter 'y' to confirm.)") |
| 111 | + if confirm.lower() == "y": |
| 112 | + try: |
| 113 | + delete_sql = "DELETE FROM vaccines WHERE id = ?" |
| 114 | + conn.execute(delete_sql,id_) |
| 115 | + result = conn.commit() #capture the result of the commit and use it to check the result |
| 116 | + if result == None: |
| 117 | + print (id_ + " " + str(delete_item[0]) + " deleted.") |
| 118 | + else: |
| 119 | + print ("Deletion failed during SQL execution.") |
| 120 | + except Error as e: |
| 121 | + print (e) |
| 122 | + pass |
| 123 | + else: |
| 124 | + print("Deletion aborted.") |
| 125 | + |
| 126 | +conn = create_connection(database_file_path) |
| 127 | +now = datetime.datetime.now() |
| 128 | + |
| 129 | +if conn: |
| 130 | + print ("Connected to database: ",conn) |
| 131 | +else: |
| 132 | + print("Error connecting to database.") |
| 133 | + |
| 134 | +while True: |
| 135 | + print("Welcome to the Vaccine Management System!") |
| 136 | + print("1 to view the data") |
| 137 | + print("2 to insert a new data record") |
| 138 | + print("3 to update a data record") |
| 139 | + print("4 to delete a data record") |
| 140 | + print("X to exit") |
| 141 | + name = input ("Choose an operation to perform: ") |
| 142 | + if (name =="1"): |
| 143 | + for row in view_data(): |
| 144 | + thisrow = " --> " |
| 145 | + for item in row: |
| 146 | + thisrow += str(item) + " " |
| 147 | + print (thisrow) |
| 148 | + elif(name == "2"): |
| 149 | + insert_data() |
| 150 | + elif(name == "3"): |
| 151 | + update_data() |
| 152 | + elif(name == "4"): |
| 153 | + delete_data() |
| 154 | + elif(name == "X"): |
| 155 | + conn.close() |
| 156 | + break |
0 commit comments