Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymysql
from vs_url_for import vs_url_for
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='mytwits_user',
passwd='mytwits_password',
db='mytwits')
def get_all_twits(self):
query = "select u.username, t.twit, t.created_at from twits t, users u where t.user_id=u.user_id order by t.created_at desc;"
with self.db.cursor() as cursor:
cursor.execute(query)
return cursor.fetchall() # The method fetches all (or all remaining) rows of a query result set and returns a list of tuples
def add_twit(self,twit):
query = "insert into twits (twit,user_id) values \
('{}','{}');".format(twit,1)
print(query)
with self.db.cursor() as cursor:
cursor.execute(query)
return self.db.commit()
app = Flask(__name__)
db = DBHelper()
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mysql_2.html", twits=twits)
@app.route('/add_twit', methods = ['GET', 'POST'])
def add_twit():
if request.form.get('twit'):
db.add_twit(request.form['twit'])
return redirect(vs_url_for('index'))
return render_template('add_twit_mysql_2.html')
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)