INSERT Example

    #Strings should be added as parameters to avoid sanatisation risks unless you know they are safe.
    #Other values can be added as parameters too or inline in the INSERT text.    
    db1cursor.execute("""INSERT INTO my_table (
                        some_value_column,
                        some_string_column
                    ) VALUES (
                        ?,
                        ?
                    )""",
                    (my_value1, my_string1));
    db1.commit()
    #if db1cursor.rowcount < 0:
    #    print("FAILED");
Important note about sanitised parameters – providing only one parameter

If you are providing just a single parameter you must include a trailing comma like below, this is to make sure the parameter field is seen as a tuple of length one.

                    (MyValue1,));

INSERT and get Auto Increment value

    #Strings should be added as parameters to avoid sanatisation risks unless you know they are safe.
    #Other values can be added as parameters too or inline in the INSERT text.    
    db1cursor.execute("""INSERT INTO my_table (
                        some_value_column,
                        some_string_column
                    ) VALUES (
                        ?,
                        ?
                    )""",
                    (my_value1, my_string1));
    new_row_id_value = db1cursor.lastrowid
    db1.commit()
    #if db1cursor.rowcount < 0:
    #    print("FAILED");

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *