Skip to content

Commit f56d461

Browse files
authored
Update to use SQLite
1 parent d925bd1 commit f56d461

File tree

7 files changed

+44
-128
lines changed

7 files changed

+44
-128
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
*.db
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

Pipfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ name = "pypi"
55

66
[packages]
77
flask = "*"
8-
flask-sqlalchemy = "*"
98

109
[dev-packages]
1110

Pipfile.lock

Lines changed: 1 addition & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/__main__.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
sys.path.append('.')
33

4-
from server.webapp import flaskapp, db, TEMPLATES
4+
from server.webapp import flaskapp, database, cursor, TEMPLATES
55
from server.models import *
66
from server.routes import *
77

@@ -15,11 +15,21 @@
1515

1616

1717
if __name__ == "__main__":
18+
cursor.execute(
19+
'''CREATE TABLE books (name text, author text, read text)'''
20+
)
21+
1822
for bookname, bookauthor, hasread in default_books:
19-
if not Book.query.filter_by(name=bookname).first():
20-
book = Book(name=bookname, author=bookauthor, read=hasread)
21-
db.session.add(book)
22-
db.session.commit()
23+
try:
24+
cursor.execute(
25+
'INSERT INTO books values (?, ?, ?)',
26+
(bookname, bookauthor, 'true' if hasread else 'false')
27+
)
28+
29+
except Exception as err:
30+
print(f'[!] Error Occurred: {err}')
2331

24-
db.create_all()
2532
flaskapp.run('0.0.0.0', debug=True)
33+
34+
cursor.close()
35+
database.close()

server/models/books.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11

2-
from server.webapp import db
2+
from dataclasses import dataclass
33

4-
5-
class Book(db.Model):
6-
id = db.Column(db.Integer, primary_key=True)
7-
name = db.Column(db.String(128), unique=False, nullable=False)
8-
author = db.Column(db.String(128), unique=False, nullable=False)
9-
10-
read = db.Column(db.Boolean(), unique=False, nullable=False)
4+
@dataclass
5+
class Book():
6+
name: str
7+
author: str
8+
read: bool

server/routes.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
from flask import request, render_template, make_response
33

4-
from server.webapp import flaskapp, db
4+
from server.webapp import flaskapp, cursor
55
from server.models import Book
66

77

@@ -12,20 +12,23 @@ def index():
1212
read = bool(request.args.get('read'))
1313

1414
if name:
15-
result = db.engine.execute(
16-
"SELECT * FROM Book WHERE name LIKE '%" + name + "%'"
15+
cursor.execute(
16+
"SELECT * FROM books WHERE name LIKE '%" + name + "%'"
1717
)
18-
books = [row for row in result]
18+
books = [Book(*row) for row in cursor]
1919
if len(books) == 0:
2020
return make_response(f"Search Result not found: {name}", 404)
21+
2122
elif author:
22-
result = db.engine.execute(
23-
"SELECT * FROM Book WHERE author LIKE '%" + author + "%'"
23+
cursor.execute(
24+
"SELECT * FROM books WHERE author LIKE '%" + author + "%'"
2425
)
25-
books = [row for row in result]
26+
books = [Book(*row) for row in cursor]
2627
if len(books) == 0:
2728
return make_response(f"Search Result not found: {author}", 404)
29+
2830
else:
29-
books = Book.query.all()
30-
31+
cursor.execute("SELECT name, author, read FROM books")
32+
books = [Book(*row) for row in cursor]
33+
3134
return render_template('books.html', books=books)

server/webapp.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import os
2+
import sqlite3
23

34
from flask import Flask
4-
from flask_sqlalchemy import SQLAlchemy
5+
56

67
ROOT = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
78
TEMPLATES = os.path.join(ROOT, 'templates')
89

910
flaskapp = Flask("BookStore", template_folder=TEMPLATES)
10-
flaskapp.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLITE_URI', 'sqlite:////tmp/test.db')
1111
flaskapp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1212

13-
db = SQLAlchemy(flaskapp)
13+
database_uri = os.environ.get('SQLITE_URI', ':memory:')
14+
15+
database = sqlite3.connect(database_uri, check_same_thread=False)
16+
cursor = database.cursor()

0 commit comments

Comments
 (0)