This dummy app on my dev server has two simple routes
@app.route('/data', methods=['GET']) def get_data(): cur = mysql.connection.cursor() cur.execute('''SELECT * FROM table_name''') data = cur.fetchall() cur.close() return jsonify(data) # Create a route to get data based on a specific ID @app.route('/data/<int:id>', methods=['GET']) def get_data_by_id(id): cur = mysql.connection.cursor() cur.execute('''SELECT * FROM table_name WHERE id = %s''', (id,)) data = cur.fetchall() cur.close() return jsonify(data)
Both work fine. I created a new route that inserts data into a local MySQL database based on query arguments like so
@app.route('/data', methods=['POST']) def add_data(): id = request.args.get('id') name = request.args.get('name') age = request.args.get('age') cur = mysql.connection.cursor() cur.execute('''INSERT INTO table_name (id, name, age) VALUES (%s, %s, %s)''', (id, name, age)) mysql.connection.commit() cur.close() return jsonify({'message': 'Data added successfully'})
When I navigate to the URL,
http://127.0.0.1:8000/data?id=2&name=jack&age=4
The INSERT runs but nothing gets inserted.
How can I pass the value for each key correctly to my INSERT?
submitted by /u/CrabEnvironmental864
[link] [comments]
r/learnpython This dummy app on my dev server has two simple routes @app.route(‘/data’, methods=[‘GET’]) def get_data(): cur = mysql.connection.cursor() cur.execute(”’SELECT * FROM table_name”’) data = cur.fetchall() cur.close() return jsonify(data) # Create a route to get data based on a specific ID @app.route(‘/data/<int:id>’, methods=[‘GET’]) def get_data_by_id(id): cur = mysql.connection.cursor() cur.execute(”’SELECT * FROM table_name WHERE id = %s”’, (id,)) data = cur.fetchall() cur.close() return jsonify(data) Both work fine. I created a new route that inserts data into a local MySQL database based on query arguments like so @app.route(‘/data’, methods=[‘POST’]) def add_data(): id = request.args.get(‘id’) name = request.args.get(‘name’) age = request.args.get(‘age’) cur = mysql.connection.cursor() cur.execute(”’INSERT INTO table_name (id, name, age) VALUES (%s, %s, %s)”’, (id, name, age)) mysql.connection.commit() cur.close() return jsonify({‘message’: ‘Data added successfully’}) When I navigate to the URL, http://127.0.0.1:8000/data?id=2&name=jack&age=4 The INSERT runs but nothing gets inserted. How can I pass the value for each key correctly to my INSERT? submitted by /u/CrabEnvironmental864 [link] [comments]
This dummy app on my dev server has two simple routes
@app.route('/data', methods=['GET']) def get_data(): cur = mysql.connection.cursor() cur.execute('''SELECT * FROM table_name''') data = cur.fetchall() cur.close() return jsonify(data) # Create a route to get data based on a specific ID @app.route('/data/<int:id>', methods=['GET']) def get_data_by_id(id): cur = mysql.connection.cursor() cur.execute('''SELECT * FROM table_name WHERE id = %s''', (id,)) data = cur.fetchall() cur.close() return jsonify(data)
Both work fine. I created a new route that inserts data into a local MySQL database based on query arguments like so
@app.route('/data', methods=['POST']) def add_data(): id = request.args.get('id') name = request.args.get('name') age = request.args.get('age') cur = mysql.connection.cursor() cur.execute('''INSERT INTO table_name (id, name, age) VALUES (%s, %s, %s)''', (id, name, age)) mysql.connection.commit() cur.close() return jsonify({'message': 'Data added successfully'})
When I navigate to the URL,
http://127.0.0.1:8000/data?id=2&name=jack&age=4
The INSERT runs but nothing gets inserted.
How can I pass the value for each key correctly to my INSERT?
submitted by /u/CrabEnvironmental864
[link] [comments]