-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.py
186 lines (164 loc) · 7.51 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from lib import *
#read and parse an XML file
ntree = ET.parse('programmingbooks.xml')
root = ntree.getroot()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#CRUD OPERATIONS (Create, Read, Update, Delete)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Show all books.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def showAllBooks():
for book in root.findall('book'):
bookid = book.find('bookid').text
author = book.find('author').text
title = book.find('title').text
genre = book.find('genre').text
price = book.find('price').text
publish_date = book.find('publish_date').text
isbn10 = book.find('isbn10').text
description = book.find('description').text
print("--------------------------------------------")
print("ID: " + bookid)
print("Author: " + author)
print("Title: " + title)
print("Genre: " + genre)
print("Price: " + price)
print("Publish Date: " + publish_date)
print("ISBN-10: " + isbn10)
print("Description: " + description)
print("--------------------------------------------")
print("End of books.")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Search for a book.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def searchForBook():
isbn10 = input("Enter the ISBN-10 of the book you want to search for: ")
for book in root.findall('book'):
isbn10A = book.find('isbn10').text
if isbn10A == isbn10:
print("ISBN-10 Requested: " + isbn10 + " ISBN-10 Found: " + isbn10A)
bookid = book.find('bookid').text
author = book.find('author').text
title = book.find('title').text
genre = book.find('genre').text
price = book.find('price').text
publish_date = book.find('publish_date').text
isbn10 = book.find('isbn10').text
description = book.find('description').text
print("--------------------------------------------")
print("ID: " + bookid)
print("Author: " + author)
print("Title: " + title)
print("Genre: " + genre)
print("Price: " + price)
print("Publish Date: " + publish_date)
print("ISBN-10: " + isbn10)
print("Description: " + description)
print("--------------------------------------------")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Edit a book.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def editBook():
isbn10 = input("Enter the ISBN-10 of the book you want to edit: ")
for book in root.findall('book'):
isbn10A = book.find('isbn10').text
if isbn10A == isbn10:
bookid = book.find('bookid').text
author = book.find('author').text
title = book.find('title').text
genre = book.find('genre').text
price = book.find('price').text
publish_date = book.find('publish_date').text
isbn10 = book.find('isbn10').text
description = book.find('description').text
# edit author
author = input("Please enter author:(" + author + ")") or author
book.find('author').text = author
# edit title
title = input("Please enter title:(" + title + ")") or title
book.find('title').text = title
# edit genre
genre = input("Please enter genre:(" + genre + ")") or genre
book.find('genre').text = genre
# edit price
price = input("Please enter price:(" + price + ")") or price
book.find('price').text = price
# edit publish_date
publish_date = input("Please enter publish_date:(" + publish_date + ")") or publish_date
book.find('publish_date').text = publish_date
# edit isbn10
isbn10 = input("Please enter isbn10:(" + isbn10 + ")") or isbn10
book.find('isbn10').text = isbn10
# edit age
description = input("Please enter description:(" + description + ")") or description
book.find('description').text = description
ntree.write("programmingbooks.xml")
print("--------------------------------------------")
print("BOOK ID: " + bookid + " UPDATED")
print("--------------------------------------------")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#New ID.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def newid1():
max = 0
for book in root.findall('book'):
id = int(book.find('bookid').text)
if id > max:
max = id
return max+1
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Book Exists.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def bookExists(isbn10):
aValue = False
for book in root.findall('book'):
isbn10Tried = book.find('isbn10').text
if isbn10Tried == isbn10:
aValue = True
return aValue
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Create a book.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def createBook():
isbn10Input = input("Enter the ISBN-10 of the book you want to search for: ")
exists = bookExists(isbn10Input)
if exists == False:
newID = newid1()
print("Create a book record...")
author = input("Author:")
title = input("Title:")
genre = input("Genre:")
price = input("Price:")
publish_date = input("Publish Date:")
isbn10 = input("ISBN-10:")
description = input("Description:")
# create a contact element at root level
newbookRec = ET.SubElement(root, "book", id=str(newID))
# add the fields into out new record
ET.SubElement(newbookRec, "bookid", name="bookid").text = str(newID)
ET.SubElement(newbookRec, "author", name="author").text = author
ET.SubElement(newbookRec, "title", name="title").text = title
ET.SubElement(newbookRec, "genre", name="genre").text = genre
ET.SubElement(newbookRec, "price", name="price").text = price
ET.SubElement(newbookRec, "publish_date", name="publish_date").text = publish_date
ET.SubElement(newbookRec, "isbn10", name="isbn10").text = isbn10
ET.SubElement(newbookRec, "description", name="description").text = description
ntree.write("programmingbooks.xml")
else:
print("--------------------------------------------")
print("Book record already exists!")
print("--------------------------------------------")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Delete a book.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def deleteBook():
deleteBook = input("Enter the ISBN-10 of the book you want to delete: ")
for book in root.findall('book'):
bookISBN = book.find('isbn10').text
if bookISBN == deleteBook:
root.remove(book)
ntree.write("programmingbooks.xml")
print("--------------------------------------------")
print("Book ISBN-10:", deleteBook, "deleted!")
print("--------------------------------------------")