{"id":4441,"date":"2023-11-27T11:38:12","date_gmt":"2023-11-27T11:38:12","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4441"},"modified":"2023-11-29T15:11:15","modified_gmt":"2023-11-29T15:11:15","slug":"insert","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/database\/sqlite\/queries\/insert","title":{"rendered":"INSERT queries"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">INSERT Example<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    #Strings should be added as parameters to avoid sanatisation risks unless you know they are safe.\n    #Other values can be added as parameters too or inline in the INSERT text.    \n    db1cursor.execute(\"\"\"INSERT INTO my_table (\n                        some_value_column,\n                        some_string_column\n                    ) VALUES (\n                        ?,\n                        ?\n                    )\"\"\",\n                    (my_value1, my_string1));\n    db1.commit()\n    #if db1cursor.rowcount &lt; 0:\n    #    print(\"FAILED\");<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Important note about sanitised parameters &#8211; providing only one parameter<\/h5>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                    (MyValue1,));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">INSERT and get Auto Increment value<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    #Strings should be added as parameters to avoid sanatisation risks unless you know they are safe.\n    #Other values can be added as parameters too or inline in the INSERT text.    \n    db1cursor.execute(\"\"\"INSERT INTO my_table (\n                        some_value_column,\n                        some_string_column\n                    ) VALUES (\n                        ?,\n                        ?\n                    )\"\"\",\n                    (my_value1, my_string1));\n    new_row_id_value = db1cursor.lastrowid\n    db1.commit()\n    #if db1cursor.rowcount &lt; 0:\n    #    print(\"FAILED\");<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>INSERT Example Important note about sanitised parameters &#8211; 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. INSERT and get Auto Increment value<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[355],"tags":[],"class_list":["post-4441","post","type-post","status-publish","format-standard","hentry","category-queries"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4441"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4441\/revisions"}],"predecessor-version":[{"id":4467,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4441\/revisions\/4467"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}