Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Insert into mysql in python but it's instantly deleted

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Looks like you forgot connection.commit() .

https://pypi.org/project/PyMySQL/#documentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading