-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectwithdatabase.py
More file actions
44 lines (40 loc) · 1.23 KB
/
connectwithdatabase.py
File metadata and controls
44 lines (40 loc) · 1.23 KB
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
from flask import Flask
from flask_mysqldb import MySQL
app=Flask(__name__)
app.config['MYSQL_HOST']='localhost'
app.config['MYSQL_PORT']='3306'
app.config['MYSQL_USER']='root'
app.config['MYSQL_PASSWORD']=''
app.config['MYSQL_DB']='alpha'
mysql=MySQL(app)
# @app.route('/')
# def home():
# return "this show how we insert data into table in database using flask"
@app.route('/create')
def create():
query="CREATE TABLE crops(cropid int primary key,cropname varchar(20),cropcolor varchar(10))"
cur=mysql.connection.cursor()
cur.execute(query)
mysql.connection.commit()
cur.close()
return "done"
# i created table name users in database login
# @app.route('/insert')
# def index():
# firstname="arslan"
# lastname="arshad"
# cur=mysql.connection.cursor()
# cur.execute("INSERT INTO users(fname,lname) VALUES(%s,%s)",(firstname,lastname))
# mysql.connection.commit()
# cur.close()
# return "inserted"
# @app.route('/fetch')
# def fetch_data():
# query="select * from book"
# cur=mysql.connection.cursor()
# cur.execute(query)
# mysql.connection.commit()
# result=cur
# for x in result:
# print(x)
app.run(debug=True)