rasbt / python-machine-learning-book-3rd-edition

The "Python Machine Learning (3rd edition)" book code repository
https://www.amazon.com/Python-Machine-Learning-scikit-learn-TensorFlow/dp/1789955750/
MIT License
4.55k stars 1.96k forks source link

Ch09 - Update the variable for the db connection in app.py #112

Closed vidyabhandary closed 4 years ago

vidyabhandary commented 4 years ago

Changed review_db to db in the SQL INSERT statement

pep8speaks commented 4 years ago

Hello @vidyabhandary! Thanks for opening this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 34:57: E502 the backslash is redundant between brackets

rasbt commented 4 years ago

Thanks for the PR. Just wondering where in the code you faced issues? Afaik running the code with review_db didn't cause any problems in my case.

vidyabhandary commented 4 years ago

I faced a problem when I ran python app.py in movieclassifier, submitted the review and clicked 'Correct' / 'Incorrect'. I got a 'NameError: name 'review_db' is not defined'. Changing the code to match the declaration of the database connection in app.py worked (at all three places - In the PR only one change is present, one more should have been present). Puzzled it is working for you ! I realized I specifically had to change it to review_db and not db.

vidyabhandary commented 4 years ago

Since the code to change is different than what it should be (in case it is a problem) I will close this PR. Thanks for the GitHub repo. I am learning a lot going through the chapter jupyter notebooks.

rasbt commented 4 years ago

I was just taking another look at the code, and it seems like "review_db" should be correct, e.g., https://github.com/rasbt/python-machine-learning-book-3rd-edition/blob/master/ch09/movieclassifier/app.py#L34

Was there some other place where it was defined as "db" instead of "review_db" -- sorry, I am currently not seeing it and want to make sure that I didn't overlook a potential issue here.

vidyabhandary commented 4 years ago

Line 18 and Line 73 - it is defined as db in the same app.py file.

rasbt commented 4 years ago

Oh I see. But that's just a Python variable name.

vidyabhandary commented 4 years ago

Yes. However since review_db was not defined in that py file anywhere else, I got an error.

rasbt commented 4 years ago

Hm, maybe you called the data base table inside review.sqlite db instead of review_db by accident? When I check the code for creating the review.sqlite file, it looks correct to me:

Screen Shot 2020-03-05 at 8 15 31 AM

This is from https://github.com/rasbt/python-machine-learning-book-3rd-edition/blob/master/ch09/ch09.ipynb

which is also mentioned in the chapter.

vidyabhandary commented 4 years ago

Checked again - all are called 'review_db'.

conn = sqlite3.connect('reviews.sqlite')
c = conn.cursor()

c.execute('DROP TABLE IF EXISTS review_db')
c.execute('CREATE TABLE review_db (review TEXT, sentiment INTEGER, date TEXT)')

example1 = 'I love this movie'
c.execute('INSERT INTO review_db (review, sentiment, date) VALUES (?, ?, DATETIME("now"))', (example1, 1))

example2 = 'I dislike this movie'
c.execute('INSERT INTO review_db (review, sentiment, date) VALUES (?, ?, DATETIME("now"))', (example2, 0))

conn.commit()
conn.close()
rasbt commented 4 years ago

ok that's great. Thanks