{"id":32174,"date":"2023-05-29T11:45:49","date_gmt":"2023-05-29T03:45:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=32174"},"modified":"2023-07-31T00:14:53","modified_gmt":"2023-07-30T16:14:53","slug":"python%e6%93%8d%e4%bd%9cmysql%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88%e8%a6%81%e5%9c%a8%e6%89%a7%e8%a1%8c%e5%90%8e%e5%8a%a0commit","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/32174.html","title":{"rendered":"python\u64cd\u4f5cmysql\u6570\u636e\u5e93\uff0c\u4e3a\u4ec0\u4e48\u8981\u5728\u6267\u884c\u540e\u52a0commit()"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>\u56e0\u4e3a\u5728\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u65f6\u5019\u662f\u5148\u5728\u7a0b\u5e8f\u4e2d\u6267\u884csql\u8bed\u53e5\uff0c\u8fd9\u79cd\u64cd\u4f5c\u5e76\u6ca1\u6709\u5728\u672c\u5730\u6570\u636e\u5e93\u4e2d\u66f4\u65b0\uff0c\u5f53\u6211\u4eec\u53ea\u60f3commit\u65f6\u624d\u4f1a\u771f\u6b63\u66f4\u65b0\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u6211\u4eec\u64cd\u4f5c\u9014\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u5bfc\u81f4\u6570\u636e\u53ea\u64cd\u4f5c\u4e00\u822c\u7684\u95ee\u9898\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-32175\" decoding=\"async\" height=\"421\" sizes=\"(max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-15-14.png?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-15-14.png?x-oss-process=image\/auto-orient,1\/format,webp 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-15-14-300x186.png 300w\" width=\"680\"\/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001python\u64cd\u4f5cmysql\u6570\u636e\u5e93\uff0c\u4e3a\u4ec0\u4e48\u8981\u5728\u6267\u884c\u540e\u52a0commit()<\/strong><\/h2>\n<p>\u56e0\u4e3a\u5728\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u65f6\u5019\u662f\u5148\u5728\u7a0b\u5e8f\u4e2d\u6267\u884csql\u8bed\u53e5\uff0c\u8fd9\u79cd\u64cd\u4f5c\u5e76\u6ca1\u6709\u5728\u672c\u5730\u6570\u636e\u5e93\u4e2d\u66f4\u65b0\uff0c\u5f53\u6211\u4eec\u53ea\u60f3commit\u65f6\u624d\u4f1a\u771f\u6b63\u66f4\u65b0\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u6211\u4eec\u64cd\u4f5c\u9014\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u5bfc\u81f4\u6570\u636e\u53ea\u64cd\u4f5c\u4e00\u822c\u7684\u95ee\u9898\u3002\u50cf\u94f6\u884c\u8f6c\u8d26\u5c31\u662f\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\uff0c\u6211\u7684\u94b1\u8f6c\u51fa\u53bb\u4e86\uff0c\u4f46\u662f\u8fd9\u4e2a\u65f6\u5019\u51fa\u73b0\u95ee\u9898\uff0c\u7a0b\u5e8f\u7ed3\u675f\u4e86\u3002\u8fd9\u4e2a\u65f6\u5019\u6211\u7684\u94b1\u5c11\u4e86\uff0c\u4f46\u662f\u6536\u6b3e\u65b9\u5374\u6ca1\u6536\u5230\u94b1\u3002<\/p>\n<h3 class=\"wp-block-heading\"><strong>\u901a\u7528\u6b65\u9aa4<\/strong><\/h3>\n<p>\u5176\u5b9e\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u901a\u7528\u6b65\u9aa4\uff0c\u90fd\u662f\u5199\u6b7b\u4e86\u7684\uff0c\u5927\u5bb6\u7167\u505a\u5c31\u884c\u3002<\/p>\n<p># 1. \u5bfc\u5165\u76f8\u5173\u5e93<\/p>\n<p>import pymysql<\/p>\n<p># 2. \u94fe\u63a5MySQL\u670d\u52a1\u5668<\/p>\n<p>db = pymysql.connect(host=\u2019localhost\u2019 , user=\u2019root\u2019 , password=\u2019******\u2019 , port=3306 ,db=\u2019spiders\u2019 , charset=\u2019utf8\u2032)<\/p>\n<p># 3. \u521b\u5efa\u4e00\u4e2acursor\u6e38\u6807\u5bf9\u8c61<\/p>\n<p>cursor = db.cursor()<\/p>\n<p># 4. \u5728\u8fd9\u4e00\u6b65\u5199\u4f60\u7684sql\u8bed\u53e5<\/p>\n<p>sql = \u2018select version()\u2019<\/p>\n<p># 5. \u6267\u884csql\u8bed\u53e5<\/p>\n<p>cursor.execute(sql)<\/p>\n<p># 6. \u65ad\u5f00\u8fde\u63a5<\/p>\n<p>db.close()<\/p>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u6574\u4e2a\u8fc7\u7a0b\u5c31\u662f\u7b2c\u56db\u6b65\u90a3\u91cc\u4e0d\u540c\u3002\u5f52\u6839\u5230\u5e95\uff0c\u5176\u5b9e\u5c31\u662f\u5199sql\u3002<\/p>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb\uff1a<\/strong><\/p>\n<h2 class=\"wp-block-heading\"><strong><br \/>\u4e8c\u3001\u6570\u636e\u5e93\u4e2d\u7684\u6982\u5ff5<\/strong><\/h2>\n<p><strong>Table<\/strong>\uff1a\u6570\u636e\u5e93\u4e2d\u7684\u8868\uff0c\u4e0b\u6587\u79f0\u201ctable\u201d\u6216\u8005\u201c\u8868\u201d\u3002<\/p>\n<p><strong>Column<\/strong>\uff1a\u8868\u4e2d\u7684\u5404\u4e2a\u5b57\u6bb5\uff0c\u4e0b\u6587\u79f0\u201ccolumn\u201d\u6216\u8005\u201c\u5217\u201d\u6216\u8005\u201c\u5b57\u6bb5\u201d\u3002<\/p>\n<p><strong>Row<\/strong>\uff1a\u8868\u4e2d\u7684\u5404\u6761\u8bb0\u5f55\uff0c\u4e0b\u6587\u79f0\u201crow\u201d\u6216\u8005\u201c\u884c\u201d<\/p>\n<p><strong>Index<\/strong>\uff1a\u8868\u4e2d\u7684\u7d22\u5f15\uff0c\u7528\u6237\u53ef\u4ee5\u5efa\u7acb\u7d22\u5f15\u4ee5\u4fbf\u52a0\u901f\u641c\u7d22\uff0c\u4f46\u662f\u7528\u6237\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528\u7d22\u5f15\uff0c\u4e0b\u6587\u79f0\u201cindex\u201d\u6216\u8005\u201c\u7d22\u5f15\u201d\u3002<\/p>\n<p><strong>View<\/strong>\uff1a\u6570\u636e\u5e93\u4e2d\u7684\u89c6\u56fe\uff0c\u4e00\u79cd\u7531\u5b9e\u9645\u7684\u8868\u5bfc\u51fa\u7684\u53ef\u89c6\u5316\u7684\u8868\uff0c\u5e76\u4e0d\u5b9e\u9645\u5b58\u50a8\u3002<\/p>\n<p>Virtual table\uff1a\u865a\u62df\u8868\u662f\u4e00\u79cd\u8868\u73b0\u5f97\u50cf\u8868\u7684\u5bf9\u8c61\uff0c\u4eceSQL\u8bed\u53e5\u7684\u89d2\u5ea6\u770b\uff0c\u865a\u8868\u53ef\u4ee5\u548c\u8868\u6216\u8005view\u4e00\u6837\u64cd\u4f5c\uff0c\u4f46\u662f\u5bf9\u865a\u62df\u8868\u7684\u67e5\u8be2\u6216\u8005\u4fee\u6539\u64cd\u4f5c\u4f1a\u8c03\u7528\u6ce8\u518c\u5728\u865a\u62df\u8868\u4e0a\u7684\u56de\u8c03\u51fd\u6570\uff0c\u865a\u62df\u8868\u673a\u5236\u4f7f\u7a0b\u5e8f\u53ef\u4ee5\u63d0\u4f9b\u7c7b\u4f3c\u4e8eSQL\u7684\u8868\u7684\u63a5\u53e3\u4f9bSQL\u8bed\u53e5\u64cd\u4f5c\u3002\u9690\u85cf\u5728\u865a\u62df\u8868\u4e0b\u7684\u6570\u636e\u7ed3\u6784\u53ef\u80fd\u662f\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u6216\u8005\u901a\u8fc7\u5373\u65f6\u8fd0\u7b97\u5f97\u51fa\u7684\u7ed3\u679c\uff0c\u6216\u8005\u662f\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\uff08\u6bd4\u5982CSV\uff09\u3002\u4e0b\u6587\u79f0\u201cvirtual table\u201d\u6216\u8005\u201c\u865a\u62df\u8868\u201d\u3002<\/p>\n<p><strong>Shadow table<\/strong>\uff1aFTS\uff08\u5168\u6587\u641c\u7d22\uff09\u4e2d\u6240\u4f7f\u7528\u7684\u6bcf\u4e2avirtual table\uff0c\u90fd\u67093-5\u4e2a\u771f\u5b9e\u7684\u6570\u636e\u5e93\u7684table\uff08\u5206\u522b\u540d\u4e3a%_content\u3001%_segdir\u3001%_segment\u3001 %_stat\u3001%_docsize\uff0c%\u662fFTS virtual table\u7684\u540d\u5b57\uff09\u6765\u5728\u5b9e\u73b0\uff0c\u8fd9\u4e9btable\u88ab\u79f0\u4e3ashadow table\u3002<\/p>\n<p><strong>Trigger<\/strong>\uff1a\u6570\u636e\u5e93\u4e2d\u7684\u89e6\u53d1\u5668\uff0c\u7531\u4fee\u6539\u6570\u636e\u5e93\u7684\u4e8b\u4ef6\u89e6\u53d1\u7684\u5b58\u50a8\u8fc7\u7a0b\uff0c\u4e0b\u6587\u79f0\u201c\u89e6\u53d1\u5668\u201d\u6216\u8005\u201ctrigger\u201d\u3002<\/p>\n<p><strong>Schema<\/strong>\uff1aSQLite\u6570\u636e\u5e93\u7684\u7ed3\u6784\uff08\u6709\u54ea\u4e9btable\/index\/view\/trigger\uff0c\u5206\u522b\u6709\u54ea\u4e9b\u5b57\u6bb5\uff09\uff0c\u4e0b\u6587\u79f0\u201cschema\u201d\u3002<\/p>\n<p><strong>Rowid<\/strong>\uff1arowid\u662fSQLite\u4e2d\u7684\u8868\u9690\u542b\u7684\u4e00\u4e2acolumn\uff0c\u662f\u5176\u5185\u90e8id\uff0c\u5728\u8be5\u8868\u4e2d\u5c11\u6570\uff0c\u662fSQLite\u4e2d\u7684\u5143\u6570\u636e\u3002<\/p>\n<p><strong>Statement<\/strong>\uff1aSQL\u8bed\u53e5\u3002<\/p>\n<p><strong>Prepared statement<\/strong>\uff1a\u7ecf\u8fc7\u201c\u9884\u5907\u201d\u7684SQL\u8bed\u53e5\uff0c\u6240\u8c13\u201c\u9884\u5907\u201d\u7c7b\u4f3c\u7f16\u8bd1\uff0c\u53ef\u4ee5\u518d\u591a\u6b21\u6267\u884c\u540c\u4e00\u8bed\u53e5\u7684\u65f6\u5019\u52a0\u901f\uff08\u8df3\u8fc7\u201c\u9884\u5907\u201d\u8fc7\u7a0b\uff09\u3002<\/p>\n<p><strong>sqlite_master<\/strong>\uff1asqlite\u6570\u636e\u5e93\u4e2d\u7ef4\u62a4\u7684\u7cfb\u7edf\u8868\uff0c\u8be5\u8868\u7684b-tree\u7684\u6839\u9875\u53f7\u6c38\u8fdc\u4e3a1\uff0c\u67095\u4e2a\u5217\uff0c\u5206\u522b\u662f\u7c7b\u578b\uff08table, view, index,trigger\uff0c\u56db\u8005\u4e4b\u4e00\uff09\u3001\u540d\u79f0\u3001\u6240\u5728\u8868\u540d\u3001\u6839\u9875\u53f7\u3001SQL\u8bed\u53e5\u3002<\/p>\n<p><strong>Journal<\/strong>\uff1a\u65e5\u5fd7<\/p>\n<p><strong>Transaction<\/strong>\uff1a\u4e8b\u52a1\u662f\u7528\u6237\u5b9a\u4e49\u7684\u4e00\u7cfb\u5217\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u8981\u4e48\u5168\u90e8\u6267\u884c\uff0c\u8981\u4e48\u5168\u90e8\u4e0d\u6267\u884c\u3002<\/p>\n<p><strong>Magic<\/strong>\u00a0<strong>string<\/strong>\uff1a\u7c7b\u4f3c\u201c\u9b54\u6570\/\u5e7b\u6570\u201d\uff0cSQLite\u6570\u636e\u5e93\u6587\u4ef6\u7279\u5f81\u5934\u3002<\/p>\n<p><strong>Fraction<\/strong><\/p>\n<p><strong>Auto-vacuum<\/strong>\uff1a\u81ea\u52a8\u6e05\u7a7a<\/p>\n<p><strong>Incremental-vacuum<\/strong><\/p>\n<p><strong>BLOB<\/strong>\uff1aBinary Large OBject<\/p>\n","protected":false},"excerpt":{"rendered":"\u56e0\u4e3a\u5728\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u65f6\u5019\u662f\u5148\u5728\u7a0b\u5e8f\u4e2d\u6267\u884csql\u8bed\u53e5\uff0c\u8fd9\u79cd\u64cd\u4f5c\u5e76\u6ca1\u6709\u5728\u672c\u5730\u6570\u636e\u5e93\u4e2d\u66f4\u65b0\uff0c\u5f53\u6211\u4eec\u53ea\u60f3commit\u65f6\u624d [&hellip;]","protected":false},"author":1,"featured_media":32175,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/32174"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=32174"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/32174\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/32175"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=32174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=32174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=32174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}