Commit f1fa42ae authored by Nirmal M.D.S's avatar Nirmal M.D.S

created insertTask.py

parent 698a53a5
from flask import Flask, request, jsonify
import mysql.connector
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
# MySQL Database Configuration
db_config = {
'host': 'localhost',
'user': 'root',
'password': '',
'database': 'rptestdb',
}
# Route to handle data submission
@app.route('/submit_data', methods=['POST'])
def submit_data():
try:
data = request.json # Assuming data is sent in JSON format
title = data['title']
description = data['description']
level = data['level']
print(title)
print(description)
print(level)
# Establish a connection to the MySQL database
try:
db_connection = mysql.connector.connect(**db_config)
cursor = db_connection.cursor()
except mysql.connector.Error as err:
print(f"Error: {err}")
cursor = None
# Insert data into the database
insert_query = "INSERT INTO task (title, description, level) VALUES (%s, %s, %s)"
values = (title, description, level)
cursor.execute(insert_query, values)
db_connection.commit()
cursor.close()
db_connection.close()
return jsonify({'message': 'Data inserted successfully'})
except Exception as e:
return jsonify({'error': str(e)})
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5002, debug=False)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment