I am having a weird issue, I’m trying to Insert into a table, using python.
The problem is that he inserts the row, but it’s instantly deleted by something, and I don’t know why.
That’s the code:
import json
import pymysql
endpoint = 'endpoint'
username = 'username'
password = 'password'
database_name = 'db_name'
def lambda_handler():
#insert path voted - call database
print("Start Request for insertPath")
connection = pymysql.connect(host=endpoint, user=username,passwd=password,
db=database_name)
print('connected')
cursor = connection.cursor()
insert_statement = "INSERT INTO Sentiero value ({0},{1},{2},{3},{4},{5},{6},
{7},'{8}'{9},'{10}','{11}')".format("NULL","NULL", "NULL", "NULL",
"NULL","NULL","NULL","NULL","NULL","NULL",'lucadefranco@gmail.com',
'Prova_Sentiero')
try:
cursor.execute(insert_statement)
except Exception as e:
print(e)
print('Exception in insert path')
else:
print('Path correctly inserted')
finally:
connection.close()
print(insert_statement)
lambda_handler()
and this is the result when I try to execute with python3 lambda_function.py:
Start Request for insertPath
connected
Path correctly inserted
INSERT INTO Sentiero value
(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'NULL',NULL,'lucadefranco@gmail.com',
'Prova_Sentiero')
So I thought it worked well, but in mysql workbench if I try to: SELECT * FROM SENTIERO
it doesn’t appear, meanwhile it auto-incremented, so I inserted but it’s deleted instantly..can someone help me? Result of Select, My Sentiero Table
>Solution :
Looks like you forgot connection.commit() .
https://pypi.org/project/PyMySQL/#documentation