ankpan18 commited on
Commit
1a1368c
·
1 Parent(s): caf8734

flask app with external mysql

Browse files
Files changed (2) hide show
  1. app.py +20 -12
  2. test.py +16 -1
app.py CHANGED
@@ -4,22 +4,30 @@ import pymysql
4
  app = Flask(__name__)
5
 
6
 
 
 
 
 
 
 
 
 
7
 
8
- db = pymysql.connect(
9
- host="bnadwttldj2i5cq9aymp-mysql.services.clever-cloud.com",
10
- user="uvfqvcypihhznd2u",
11
- port=3306,
12
- password="CX6TBadRQYFqprozqDTo",
13
- database="bnadwttldj2i5cq9aymp"
14
- )
15
 
16
  @app.route("/")
17
  def index():
18
- # cursor = db.cursor()
19
- # cursor.execute("SELECT * FROM users")
20
- # data = cursor.fetchall()
21
- # return render_template("index.html", data=data)
22
- return render_template("index.html")
 
 
 
 
 
 
 
23
 
24
  if __name__ == "__main__":
25
  app.run(host="0.0.0.0", port=5000)
 
4
  app = Flask(__name__)
5
 
6
 
7
+ def get_connection():
8
+ return pymysql.connect(
9
+ host="bnadwttldj2i5cq9aymp-mysql.services.clever-cloud.com",
10
+ user="uvfqvcypihhznd2u",
11
+ port=3306,
12
+ password="CX6TBadRQYFqprozqDTo",
13
+ database="bnadwttldj2i5cq9aymp"
14
+ )
15
 
 
 
 
 
 
 
 
16
 
17
  @app.route("/")
18
  def index():
19
+
20
+ db = get_connection()
21
+ cursor = db.cursor()
22
+
23
+
24
+ cursor.execute("SELECT * FROM users")
25
+ data = cursor.fetchall()
26
+
27
+ cursor.close()
28
+ db.close()
29
+
30
+ return render_template("index.html", data=data)
31
 
32
  if __name__ == "__main__":
33
  app.run(host="0.0.0.0", port=5000)
test.py CHANGED
@@ -19,4 +19,19 @@ mydb = mysql.connector.connect(
19
  database=DBNAME
20
  )
21
 
22
- print(mydb)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  database=DBNAME
20
  )
21
 
22
+ print(mydb)
23
+
24
+ cursor=mydb.cursor()
25
+ cursor.execute("drop table if exists users;")
26
+ cursor.execute(''' CREATE TABLE if not exists users (
27
+ id INT PRIMARY KEY,
28
+ name VARCHAR(255),
29
+ email VARCHAR(255)
30
+ ); ''')
31
+
32
+ cursor.execute(" INSERT INTO users VALUES (1, 'Alice', 'alice@example.com'); ")
33
+ cursor.execute(" INSERT INTO users VALUES (2, 'Bob', 'bob@example.com'); ")
34
+ cursor.execute(" INSERT INTO users VALUES (3, 'Charlie', 'charlie@example.com'); ")
35
+
36
+
37
+ mydb.commit()