Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit 4101927

Browse files
authoredFeb 6, 2019
Merge pull request #10 from jaysgrant/develop
Merge 0.1.3 into master
2 parents 7ab97a7 + 8044ed7 commit 4101927

File tree

8 files changed

+96
-29
lines changed

8 files changed

+96
-29
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ fabric.properties
6868
# Android studio 3.1+ serialized cache file
6969
.idea/caches/build_file_checksums.serl
7070
venv
71+
macvenv
7172
__pycache__
7273

‎.idea/misc.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/pydvd.iml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# PYDVD
22

33
PyDVD is an application that was developed purely to progress skills in the usage of SQLite3
4-
as a database engine used with Python 3.
4+
as a database engine used with Python 3. Development work has been performed using PyCharm as the selected IDE.
5+
6+
##### Version: 0.1.3
57

6-
##### Version: 0.1.1
78

89
## Getting Started
910

‎config/config.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[main]
22
appname = PyDVD
3-
appversion = 0.1.1
3+
appversion = 0.1.3
44
datasource = data/pydvd.db

‎pydvd.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ def main_menu():
3131
print('X. Exit PyDVD.')
3232

3333
while "the answer is invalid":
34-
reply = str(input('Select your desired action (1-5, or x to exit): ')).lower().strip()
34+
reply = str(input('Select your desired action (1-6, or x to exit): ')).lower().strip()
3535
if reply[:1] == '1':
3636
insert_record()
3737
main()
3838
if reply[:1] == '2':
3939
query_all()
4040
main()
4141
if reply[:1] == '3':
42-
conditional_query()
42+
film_query()
4343
main()
4444
if reply[:1] == '4':
4545
record_update()
@@ -100,7 +100,7 @@ def record_update():
100100
print(e)
101101
if question_answer[:1] == 'g':
102102
field_name = 'film_genre'
103-
field_value = str(input('New film genre: '))
103+
field_value = get_genre_name()
104104
try:
105105
pydvd_utils.record_update(field_name, field_value, record_id)
106106
main()
@@ -120,38 +120,37 @@ def yes_no(question):
120120

121121

122122
def insert_record():
123-
name = str(input('Name of movie: '))
124-
genre = str(input('Genre of movie: '))
125-
date = NOW
126-
table_name = 'film_inv'
127-
128123
try:
129-
pydvd_utils.record_insert(table_name, name, genre, date)
124+
name = str(input('Name of movie: '))
125+
date = NOW
126+
film_table = 'film_inv'
127+
genre_name = get_genre_name()
128+
pydvd_utils.record_insert(film_table, name, genre_name, date)
130129
except sqlite3.Error as e:
131130
print(e)
132131
except Exception as e:
133132
print(e)
134133

135134

136-
def conditional_query():
135+
def film_query():
137136
question = 'Field to search, either name or genre,'
138137
while "the answer is invalid":
139138
question_answer = str(input(question + ' (n/g): ')).lower().strip()
140139
if question_answer[:1] == 'n':
141140
field_name = 'film_name'
142141
field_value = str(input('Film name to search for: '))
143142
try:
144-
pydvd_utils.conditional_query(field_name, field_value)
143+
pydvd_utils.film_query(field_name, field_value)
145144
main()
146145
except sqlite3.Error as e:
147146
print(e)
148147
except Exception as e:
149148
print(e)
150149
if question_answer[:1] == 'g':
151150
field_name = 'film_genre'
152-
field_value = str(input('Genre to search for: '))
151+
field_value = get_genre_name()
153152
try:
154-
pydvd_utils.conditional_query(field_name, field_value)
153+
pydvd_utils.film_query(field_name, field_value)
155154
main()
156155
except sqlite3.Error as e:
157156
print(e)
@@ -177,6 +176,22 @@ def csv_export():
177176
print(e)
178177

179178

179+
def get_genre_name():
180+
genre_table = 'genre_names'
181+
print('Select the ID of the genre for your movie form the list below.')
182+
all_rows = pydvd_utils.cond_query_all(genre_table)
183+
for row in all_rows:
184+
print(row)
185+
while True:
186+
try:
187+
genre = int(input('Genre of movie: '))
188+
break
189+
except:
190+
print("Please enter a numerical value.")
191+
genre_name = str(pydvd_utils.get_genre_name(genre))
192+
return genre_name
193+
194+
180195
def main():
181196
check_setup(subdir=os.path.join(os.path.dirname(__file__), 'data'))
182197
main_menu()

‎pydvd_utils.py

+34-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import sqlite3
66
from configparser import ConfigParser
77
import csv
8-
import fileinput
98

109
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config/config.ini')
1110

@@ -32,20 +31,26 @@ def record_insert(table_name, name, genre, date):
3231
con.close()
3332

3433

34+
def cond_query_all(table_name):
35+
con = db_connect()
36+
cur = con.cursor()
37+
cur.execute('''SELECT * from ''' + table_name)
38+
all_rows = cur.fetchall()
39+
return all_rows
40+
41+
3542
def query_all():
3643
con = db_connect()
3744
cur = con.cursor()
3845
print('\n')
39-
# todo The line below should return the column names, but currently does not work.
40-
# cur.execute('PRAGMA table_info(film_inv)')
4146
cur.execute('''SELECT film_id, film_name, film_genre, date_added FROM film_inv''')
4247
all_rows = cur.fetchall()
4348
for row in all_rows:
4449
print('{0} : {1}, {2}, {3}'.format(row[0], row[1], row[2], row[3]))
4550
con.close()
4651

4752

48-
def conditional_query(field_name, field_value):
53+
def film_query(field_name, field_value):
4954
con = db_connect()
5055
cur = con.cursor()
5156
cur.execute('''SELECT film_id, film_name, film_genre, date_added FROM film_inv WHERE '''
@@ -84,11 +89,14 @@ def csv_export():
8489
all_rows = cur.fetchall()
8590
with open(filename, 'w') as f:
8691
writer = csv.writer(f)
87-
writer.writerow(['Film ID', 'Film Name', 'Film Genre', 'Date Added'])
92+
columns = get_column_names()
93+
writer.writerow(columns)
8894
writer.writerows(all_rows)
8995
f.close()
9096
con.close()
9197
remove_empty_lines(filename)
98+
csv_path = os.path.dirname(os.path.abspath(filename))
99+
print('File saved to', csv_path)
92100

93101

94102
def remove_empty_lines(filename):
@@ -97,8 +105,28 @@ def remove_empty_lines(filename):
97105
return
98106
with open(filename) as filehandle:
99107
lines = filehandle.readlines()
100-
101108
with open(filename, 'w') as filehandle:
102109
lines = filter(lambda x: x.strip(), lines)
103110
filehandle.writelines(lines)
104111

112+
113+
def get_column_names():
114+
column_names = []
115+
con = db_connect()
116+
cur = con.cursor()
117+
cur.execute("PRAGMA table_info(film_inv)")
118+
data_extract = cur.fetchall()
119+
for column in data_extract:
120+
column_names.append(column[1])
121+
column_names = [s.replace('_', ' ') for s in column_names]
122+
column_names = [s.title() for s in column_names]
123+
return column_names
124+
125+
126+
def get_genre_name(record_id):
127+
con = db_connect()
128+
cur = con.cursor()
129+
cur.execute('''SELECT genre_name FROM genre_names WHERE genre_id =? ''', (record_id,))
130+
genre_name = cur.fetchone()
131+
genre_name = ''.join(genre_name)
132+
return genre_name

‎setup.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def db_connect():
2525
return con
2626

2727

28-
def main():
28+
def db_setup():
2929
try:
3030
subdir = os.path.join(os.path.dirname(__file__), 'data')
3131
create_dir(subdir)
@@ -36,13 +36,28 @@ def main():
3636
con = db_connect()
3737
cur = con.cursor()
3838
sql_create_film_inv_table = (" CREATE TABLE IF NOT EXISTS film_inv (\n"
39-
"film_id integer PRIMARY KEY AUTOINCREMENT UNIQUE Not NULL, \n"
40-
"film_name varchar Not NULL,\n"
41-
"film_genre varchar,\n"
42-
"date_added datetime\n"
43-
"); ")
39+
"film_id integer PRIMARY KEY AUTOINCREMENT UNIQUE Not NULL, \n"
40+
"film_name varchar Not NULL,\n"
41+
"film_genre varchar,\n"
42+
"date_added datetime\n"
43+
"); ")
4444
cur.execute(sql_create_film_inv_table)
4545
con.commit()
46+
47+
sql_create_genre_table = (" CREATE TABLE IF NOT EXISTS genre_names (\n"
48+
"genre_id integer PRIMARY KEY AUTOINCREMENT UNIQUE Not NULL, \n"
49+
"genre_name varchar Not NULL\n"
50+
"); ")
51+
cur.execute(sql_create_genre_table)
52+
con.commit()
53+
54+
genres = (
55+
"Action", "Documentary", "Animated", "Drama", "Biography", "Family", "Comedy", "Fantasy", "Crime", "Horror",
56+
"Musical", "Romance", "Romantic Comedy", "Science Fiction", "Thriller", "War", "Western", "Sports",)
57+
for genre in genres:
58+
cur.execute("INSERT INTO genre_names(genre_name) VALUES(?)", [genre])
59+
con.commit()
60+
4661
except sqlite3.Error as e:
4762
print(e)
4863
except Exception as e:
@@ -52,6 +67,9 @@ def main():
5267
con.close()
5368

5469

70+
def main():
71+
db_setup()
72+
73+
5574
if __name__ == "__main__":
5675
main()
57-

0 commit comments

Comments
 (0)
This repository has been archived.