前提・実現したいこと
PythonとMySQLを接続し、
Pythonで作成したウェブアプリの
検索履歴をMySQLの上で
閲覧したいと思っています。
(使用教材:Head First Python)
発生している問題・エラーメッセージ
コードを書き終わり実行しようとするとき、
File "Hello.py", line 23 conn.commit() ^ SyntaxError: invalid syntax
というメッセージが表示され、
エラーが起きました。
しかし、自分としてはこのコードの前に
教材通りにコードを書いており、
なぜ無効なのかわかりません。
該当のソースコード
Python
1from flask import Flask, render_template, request, escape 2from vsearch import search4letters 3 4app = Flask(__name__) 5 6def log_request(req: 'flask_request', res: str) -> None: 7 dbconfig = {'host': '127.0.0.1', 8 'user': 'vsearch', 9 'password': 'vsearchpasswd', 10 'database': 'vsearchlogDB',} 11 import mysql.connector 12 conn = mysql.connector.connect(**dbconfig) 13 cursor = conn.curosr() 14 _SQL = """insert into log 15(phrase, letters, ip, browser_string, results) 16values 17(%s, %s, %s, %s, %s, %s)""" 18 cursor.execute(_SQL, (req.form['phrase'], 19 (req.form['letters'], 20 req.remote_addr, 21 req.user_agent.browser, 22 res,)) 23 conn.commit() 24 conn.close() 25 cursor.close() 26 27@app.route('/search4', methods=['POST']) 28def do_search() -> 'html': 29 """Extract the posted data; perform the search; return results.""" 30 phrase = request.form['phrase'] 31 letters = request.form['letters'] 32 title = 'Here are your results:' 33 results = str(search4letters(phrase, letters)) 34 log_request(request, results) 35 return render_template('results.html', 36 the_title=title, 37 the_phrase=phrase, 38 the_letters=letters, 39 the_results=results,) 40 41 42@app.route('/') 43@app.route('/entry') 44def entry_page() -> 'html': 45 """Display this webapp's HTML form.""" 46 return render_template('entry.html', 47 the_title='Welcome to search4letters on the web!') 48 49 50@app.route('/viewlog') 51def view_the_log() -> 'html': 52 """Display the contents of the log file as a HTML table.""" 53 contents = [] 54 with open('vsearch.log') as log: 55 for line in log: 56 contents.append([]) 57 for item in line.split('|'): 58 contents[-1].append(escape(item)) 59 titles = ('Form Data', 'Remote_addr', 'User_agent', 'Results') 60 return render_template('viewlog.html', 61 the_title='View Log', 62 the_row_titles=titles, 63 the_data=contents,) 64 65 66if __name__ == '__main__': 67 app.run(debug=True) 68
補足情報(FW/ツールのバージョンなど)
Python3.7
MySQL8.0
よろしくお願い致します。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/18 08:38 編集
2019/06/18 09:16
2019/06/18 09:16
2019/06/19 02:41
2019/06/19 02:43
2019/06/19 02:56