Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2021-210
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2021-210
2021-210
Commits
4194e761
Commit
4194e761
authored
Oct 15, 2021
by
shalithadeshan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add database api file
parent
15ec85bc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
163 additions
and
0 deletions
+163
-0
DbAPI.py
DbAPI.py
+163
-0
No files found.
DbAPI.py
0 → 100644
View file @
4194e761
import
json
import
os
from
flask
import
Flask
,
request
from
flask_cors
import
CORS
,
cross_origin
import
mysql.connector
mydb
=
mysql
.
connector
.
connect
(
host
=
"localhost"
,
user
=
"root"
,
password
=
""
,
database
=
"dog_research"
)
mycursor
=
mydb
.
cursor
()
app
=
Flask
(
__name__
)
cors
=
CORS
(
app
)
uploadPath
=
"uploads"
@
app
.
route
(
'/predict'
,
methods
=
[
'POST'
])
@
cross_origin
()
def
uploadImage
():
print
(
os
.
getcwd
())
f
=
request
.
files
[
'file'
]
splits
=
f
.
filename
.
split
(
"/"
)
print
(
splits
[
len
(
splits
)
-
1
])
# os.remove(os.path.join(uploadPath, "upload.jpg"))
f
.
save
(
os
.
path
.
join
(
uploadPath
,
"upload.jpg"
))
@
app
.
route
(
"/signup"
,
methods
=
[
'POST'
])
def
signup
():
data
=
request
.
get_json
()
print
(
data
)
sql
=
"INSERT INTO users (email, password) VALUES (
%
s,
%
s)"
val
=
(
data
[
'email'
],
data
[
'password'
])
mycursor
.
execute
(
sql
,
val
)
mydb
.
commit
()
mycursor
.
execute
(
"SELECT id FROM users ORDER BY id DESC LIMIT 1"
)
# myresult = mycursor.fetchall()
id
=
0
for
(
id
)
in
mycursor
:
# id = row['id']
id
=
id
print
(
id
)
return
{
"id"
:
id
[
0
]}
@
app
.
route
(
"/login"
,
methods
=
[
'POST'
])
def
login
():
data
=
request
.
get_json
()
print
(
data
)
id
=
0
try
:
mycursor
.
execute
(
"SELECT id FROM users WHERE email='"
+
data
[
'email'
]
+
"' AND password='"
+
data
[
'password'
]
+
"'"
)
# myresult = mycursor.fetchall()
for
(
id
)
in
mycursor
:
# id = row['id']
id
=
id
print
(
id
)
return
{
"id"
:
id
[
0
]}
except
:
return
{
"id"
:
0
}
@
app
.
route
(
"/addPet"
,
methods
=
[
'POST'
])
def
addPet
():
data
=
request
.
get_json
()
print
(
data
)
sql
=
"INSERT INTO pets (name, age, weight, gender, breed) VALUES (
%
s,
%
s,
%
s,
%
s,
%
s)"
val
=
(
data
[
'name'
],
data
[
'age'
],
data
[
'weight'
],
data
[
'gender'
],
data
[
'breed'
])
mycursor
.
execute
(
sql
,
val
)
mydb
.
commit
()
mycursor
.
execute
(
"SELECT id FROM pets ORDER BY id DESC LIMIT 1"
)
# myresult = mycursor.fetchall()
id
=
0
for
(
id
)
in
mycursor
:
# id = row['id']
id
=
id
print
(
id
)
return
{
"id"
:
id
[
0
]}
@
app
.
route
(
"/addQuestion"
,
methods
=
[
'POST'
])
def
addQuestion
():
data
=
request
.
get_json
()
print
(
data
)
sql
=
"INSERT INTO questions (question, description, tag, votes) VALUES (
%
s,
%
s,
%
s,
%
s)"
val
=
(
data
[
'question'
],
data
[
'description'
],
data
[
'tag'
],
data
[
'votes'
])
mycursor
.
execute
(
sql
,
val
)
mydb
.
commit
()
mycursor
.
execute
(
"SELECT id FROM questions ORDER BY id DESC LIMIT 1"
)
# myresult = mycursor.fetchall()
id
=
0
for
(
id
)
in
mycursor
:
# id = row['id']
id
=
id
print
(
id
)
return
{
"id"
:
id
[
0
]}
@
app
.
route
(
"/setQuestionVote"
,
methods
=
[
'POST'
])
def
setQuestionVote
():
data
=
request
.
get_json
()
print
(
data
)
sql
=
"UPDATE questions SET votes = "
+
str
(
data
[
'votes'
])
+
" WHERE id ="
+
str
(
data
[
'id'
])
mycursor
.
execute
(
sql
)
mydb
.
commit
()
return
{
"task"
:
"Updated"
}
@
app
.
route
(
"/addReview"
,
methods
=
[
'POST'
])
def
addReview
():
data
=
request
.
get_json
()
print
(
data
)
sql
=
"INSERT INTO reviews (review, post_id) VALUES (
%
s,
%
s)"
val
=
(
data
[
'review'
],
data
[
'post_id'
])
mycursor
.
execute
(
sql
,
val
)
mydb
.
commit
()
mycursor
.
execute
(
"SELECT id FROM reviews ORDER BY id DESC LIMIT 1"
)
# myresult = mycursor.fetchall()
id
=
0
for
(
id
)
in
mycursor
:
# id = row['id']
id
=
id
print
(
id
)
return
{
"id"
:
id
[
0
]}
@
app
.
route
(
"/getPets"
,
methods
=
[
'GET'
,
'POST'
])
def
getPets
():
try
:
mycursor
.
execute
(
"SELECT * FROM pets"
)
myresult
=
mycursor
.
fetchall
()
return
json
.
dumps
(
myresult
)
except
:
return
{
"id"
:
0
}
@
app
.
route
(
"/getQuestions"
,
methods
=
[
'GET'
,
'POST'
])
def
getQuestions
():
try
:
mycursor
.
execute
(
"SELECT * FROM questions"
)
myresult
=
mycursor
.
fetchall
()
return
json
.
dumps
(
myresult
)
except
:
return
{
"id"
:
0
}
@
app
.
route
(
"/getReviews"
,
methods
=
[
'GET'
,
'POST'
])
def
getReviews
():
try
:
mycursor
.
execute
(
"SELECT * FROM reviews"
)
myresult
=
mycursor
.
fetchall
()
return
json
.
dumps
(
myresult
)
except
:
return
{
"id"
:
0
}
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
,
port
=
5005
,
host
=
"0.0.0.0"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment