Skip to content

Commit 5d9d48c

Browse files
committed
Clean. SDK work
1 parent 7192bb6 commit 5d9d48c

3 files changed

Lines changed: 32 additions & 44 deletions

File tree

‎python/10-sql/10-sql.py‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sqlite3 #included with Python
44

5-
conn = sqlite3.connect(os.path.realpath(":memory:"))
5+
conn = sqlite3.connect(":memory:")
66

77
#creating this goes off of the cwd. CD in terminal changes cwd
88
#so either cd to correct folder or figure out how to do a more concrete path:
@@ -72,7 +72,24 @@
7272

7373
########## Create an SDK ##########
7474

75-
import booksAPI
76-
"""
75+
76+
import booksSDK
77+
from book import Book
78+
79+
book = Book("Are You My Mother?", 1000)
80+
print("Added book id:", booksAPI.add_book(book))
81+
print("Get book by title:", booksAPI.get_book_by_title(book.title))
82+
print("Not a valid book:", booksAPI.get_book_by_title("yeet"))
83+
84+
booksAPI.add_book(Book('The Digging-est Dog', 76))
85+
print("get books:", booksAPI.get_books())
86+
87+
book = booksAPI.update_book(book, book.title, 76)
88+
print("Updated book:", book)
89+
booksAPI.delete_book(book)
90+
print("After delete:", booksAPI.get_book_by_title("Are You My Mother?"))
91+
92+
print("All books:", booksAPI.get_books())
93+
7794

7895

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
#execute init.py first to get proper db structure
2-
#delete db and re-run init.py as needed
1+
32
from book import Book
43
import sqlite3
54
import os.path
65

6+
l = os.path.realpath(
7+
os.path.join(os.getcwd(), os.path.dirname(__file__)))
8+
9+
conn = sqlite3.connect(os.path.join(l, 'books.db'))
10+
11+
c = conn.cursor()
12+
# Create table
13+
c.execute('''CREATE TABLE IF NOT EXISTS books
14+
(title TEXT, pages INTEGER)''')
15+
716
def cursor():
817
#https://stackoverflow.com/questions/14511337/efficiency-of-reopening-sqlite-database-after-each-query/14520670
918

@@ -50,25 +59,4 @@ def delete_book(book):
5059
c = cursor()
5160
with c.connection:
5261
c.execute('DELETE FROM books WHERE title=? AND pages=?', (book.title, book.pages))
53-
c.connection.close()
54-
55-
56-
book = Book("Are You My Mother?", 1000)
57-
print("Added book id:", add_book(book))
58-
print("Get book by title:", get_book_by_title(book.title))
59-
print("Not a valid book:", get_book_by_title("yeet"))
60-
61-
add_book(Book('The Digging-est Dog', 76))
62-
print("get books:", get_books())
63-
64-
book = update_book(book, book.title, 76)
65-
print("Updated book:", book)
66-
delete_book(book)
67-
print("After delete:", get_book_by_title("Are You My Mother?"))
68-
69-
print("All books:", get_books())
70-
71-
72-
73-
74-
62+
c.connection.close()

‎python/10-sql/init.py‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)