{"id":707,"date":"2021-11-21T08:17:00","date_gmt":"2021-11-21T08:17:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=707"},"modified":"2021-11-21T08:17:00","modified_gmt":"2021-11-21T08:17:00","slug":"707","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/11\/21\/707\/","title":{"rendered":"Python\u5b89\u88c5 MySQL Connector \u9a71\u52a8\u7a0b\u5e8f\uff0c\u8bbf\u95ee MySQL \u6570\u636e\u5e93\u548c\u67e5\u8be2\u4fee\u6539 WordPress \u535a\u5ba2\u6587\u7ae0"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/3729448974.webp\" alt=\"mysql.webp\" title=\"mysql.webp\"><\/p>\n<h2>MySQL \u662f\u6700\u53d7\u6b22\u8fce\u7684\u6570\u636e\u5e93\u4e4b\u4e00\uff0cPython \u9700\u8981 MySQL \u9a71\u52a8\u7a0b\u5e8f\u6765\u8bbf\u95ee MySQL \u6570\u636e\u5e93<\/h2>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u5c06\u4f7f\u7528\u9a71\u52a8\u7a0b\u5e8f <code>MySQL Connector<\/code>\uff0c\u5efa\u8bae\u60a8\u4f7f\u7528 PIP \u5b89\u88c5 <code>MySQL Connector<\/code><\/p>\n<h3>\u5b89\u88c5 MySQL \u9a71\u52a8\u7a0b\u5e8f <code>mysql-connector<\/code><\/h3>\n<pre><code>pip install mysql-connector<\/code><\/pre>\n<h3>\u5982\u679c\u56fd\u5185\u7f51\u901f\u6162\uff0c\u53ef\u4ee5pip \u6307\u5b9a\u56fd\u5185\u6e90\u5b89\u88c5: <code>-i URL<\/code> \uff0c\u8fd9\u91cc\u6307\u5b9a\u6e05\u534e\u6e90<\/h3>\n<pre><code>pip install mysql-connector \\r\n  -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple\/<\/code><\/pre>\n<h2>\u6d4b\u8bd5 MySQL Connector, \u4f7f\u7528\u5185\u7f6e\u51fd\u6570 dir() \u7528\u4e8e\u67e5\u627e <code>mysql.connector<\/code> \u6a21\u5757\u5b9a\u4e49\u7684\u540d\u79f0<\/h2>\n<pre><code>import mysql.connector<\/code><\/pre>\n<p>\u5982\u679c\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u6ca1\u6709\u9519\u8bef\uff0c\u5219 <code>MySQL Connector<\/code> \u5df2\u5b89\u88c5\u5e76\u5f85\u7528\u3002<\/p>\n<pre><code>&gt;&gt;&gt; dir(mysql.connector)\n['BINARY', 'Binary', 'CharacterSet', 'ClientFlag', 'Connect', 'DATETIME', 'DataError', 'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'FieldFlag', 'FieldType', 'HAVE_CEXT', 'IntegrityError', 'InterfaceError', 'InternalError', 'MySQLConnection', 'NUMBER', 'NotSupportedError', 'OperationalError', 'PoolError', 'ProgrammingError', 'ROWID', 'RefreshOption', 'STRING', 'Time', 'TimeFromTicks', 'Timestamp', 'TimestampFromTicks', 'Warning', '_CONNECTION_POOLS', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '__version_info__','_get_failover_connection', '_get_pooled_connection', 'abstracts', 'apilevel', 'authentication', 'catch23', 'charsets', 'connect', 'connection', 'constants', 'conversion', 'cursor', 'custom_error_exception', 'custom_types', 'dbapi', 'errorcode', 'errors', 'locales', 'network', 'optionfiles', 'paramstyle', 'protocol', 'read_option_files', 'threadsafety', 'utils', 'version']<\/code><\/pre>\n<h2>\u9996\u5148\u521b\u5efa\u4e0e MySql\u6570\u636e\u5e93\u7684\u8fde\u63a5<\/h2>\n<pre><code>import mysql.connector\n\nmydb = mysql.connector.connect(\n  host=\"localhost\",\n  user=\"root\",\n  passwd=\"yourpassword\"\n)\n\nprint(mydb)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/1874440696.png\" alt=\"python-mysql.png\" title=\"python-mysql.png\"><\/p>\n<h2>\u4f7f\u7528 <code>SHOW DATABASES<\/code> \u8bed\u53e5\u5217\u51fa\u7cfb\u7edf\u4e2d\u7684\u6240\u6709\u6570\u636e\u5e93<\/h2>\n<pre><code>c = mydb.cursor()\nc.execute(\"SHOW DATABASES\")\nfor x in c:\n  print(x)<\/code><\/pre>\n<h3>\u672c\u6587\u4f7f\u7528\u6d4b\u8bd5\u4f7f\u7528\u5efa\u7acb\u7684\u6570\u636e\u5e93\uff0c\u4f60\u4e5f\u53ef\u4ee5\u53c2\u8003\u6587\u7ae0\u5148\u5efa\u7acb\u6570\u636e\u5e93: <a href=\"https:\/\/lyvba.com\/index.php\/archives\/124\/\">\u4e00\u952e WordPress \u535a\u5ba2\u5b89\u88c5\u811a\u672c\uff0c\u540c\u65f6\u4e5f\u662f LNMP \u5957\u4ef6<\/a><\/h3>\n<h2>WordPress \u5b58\u653e\u6587\u7ae0\u7684\u6570\u636e\u8868\u540d <code>wp_posts<\/code>\uff0c\u4f7f\u7528 <code>SELECT<\/code> \u67e5\u8be2\u535a\u5ba2\u7684\u6587\u7ae0<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/2488248789.png\" alt=\"USE_SELECT.png\" title=\"USE_SELECT.png\"><\/p>\n<pre><code>c = mydb.cursor()\nc.execute(\"USE wordpress\")\nc.execute(\"SELECT * FROM wp_posts LIMIT 5\")\nresult = c.fetchall()\nfor x in result:\n  print(x)<\/code><\/pre>\n<h3>\u9009\u53d6\u5217-\u53ea\u9009\u62e9\u8868\u4e2d\u7684\u67d0\u4e9b\u5217\uff0c\u8bf7\u4f7f\u7528 <code>SELECT<\/code> \u8bed\u53e5\uff0c\u540e\u8ddf\u5217\u540d<\/h3>\n<pre><code>&gt;&gt;&gt; c.execute(\"SELECT ID, post_title, guid, post_type  FROM wp_posts LIMIT 5\")\n&gt;&gt;&gt; result = c.fetchall()\n&gt;&gt;&gt; for x in result: print(x)\n...\n(1, '\u4e16\u754c\uff0c\u5927\u5bb6\u597d\uff01', 'https:\/\/lyvba.com\/wordpress\/?p=1', 'post')\n(2, '\u793a\u4f8b\u9875\u9762', 'https:\/\/lyvba.com\/wordpress\/?page_id=2', 'page')\n(3, '\u9690\u79c1\u653f\u7b56', 'https:\/\/lyvba.com\/wordpress\/?page_id=3', 'page')\n(20, '\u81ea\u52a8\u8349\u7a3f', 'https:\/\/lyvba.com\/wordpress\/?p=20', 'post')\n(21, 'Nginx\u8bbf\u95ee\u63a7\u5236allow\u3001deny\uff08ngx_http_access_module\uff09', 'https:\/\/lyvba.com\/wordpress\/?p=21', 'post')<\/code><\/pre>\n<h3>\u7528 <code>fetchall()<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4ece\u6700\u540e\u6267\u884c\u7684\u8bed\u53e5\u4e2d\u83b7\u53d6\u6240\u6709\u884c, \u4f7f\u7528 <code>LIMIT<\/code> \u8bed\u53e5\u9650\u5236\u4ece\u67e5\u8be2\u8fd4\u56de\u7684\u8bb0\u5f55\u6570<\/h3>\n<h3>\u5982\u679c\u53ea\u9700\u4e00\u884c\u6216\u8005\u9010\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>fetchone()<\/code> \u65b9\u6cd5, \u5c06\u8fd4\u56de\u7ed3\u679c\u7684\u7b2c\u4e00\u884c<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/936992067.png\" alt=\"getone.PNG\" title=\"getone.PNG\"><\/p>\n<pre><code>c.execute(\"SELECT ID, post_title, guid, post_type  FROM wp_posts LIMIT 5\")\nresult = c.fetchone()\nprint(result)\nresult = c.fetchone();  print(result)<\/code><\/pre>\n<h2>WordPress\u6587\u7ae0\u5185\u5bb9\u6279\u91cf\u66ff\u6362\u6587\u5b57\u7684\u65b9\u6cd5<\/h2>\n<ul>\n<li>\u76f8\u5173\u6587\u7ae0:<a href=\"https:\/\/lyvba.com\/index.php\/archives\/711\/\">WordPress\u7f51\u7ad9\u66f4\u6539\u57df\u540d\u540e\u6587\u7ae0\u53ef\u4ee5\u4f1a\u51fa\u73b0guid\u95ee\u9898\u89e3\u51b3\u65b9\u6cd5<\/a><\/li>\n<li>\n<p><a href=\"https:\/\/lyvba.com\/index.php\/archives\/702\/\">WordPress\u548cTypecho\u6587\u7ae0\u5185\u5bb9\u6279\u91cf\u66ff\u6362\u6587\u5b57\u7684\u65b9\u6cd5<\/a><\/p>\n<h3>WordPress \u5b58\u653e\u6587\u7ae0\u7684\u6570\u636e\u8868\u540d <code>wp_posts<\/code> \uff0c\u5b57\u6bb5\u540d\u79f0\u4e3a <code>post_content<\/code><\/h3>\n<pre><code>#  UPDATE wp_posts SET post_content = replace(post_content,'\u65e7\u8bcd\u8bed','\u65b0\u8bcd\u8bed');\nsql = \"UPDATE wp_posts SET post_content = replace(post_content,'nginx','nginx-php')\"\nc.execute(sql)\nmydb.commit()\nprint(c.rowcount, \"record(s) affected\")<\/code><\/pre>\n<\/li>\n<li>\u66f4\u65b0\u8868: \u4f7f\u7528 <code>UPDATE<\/code> \u8bed\u53e5\u6765\u66f4\u65b0\u8868\u4e2d\u7684\u73b0\u6709\u8bb0\u5f55<\/li>\n<li>\u91cd\u8981\uff1a<code>commit()<\/code> \u8fd9\u4e2a\u65b9\u6cd5\u63d0\u4ea4\u5f53\u524d\u4e8b\u52a1\u3002\u5982\u679c\u6ca1\u6709\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u90a3\u4e48\u4ece\u4e0a\u4e00\u6b21\u63d0\u4ea4 commit() \u4ee5\u6765\u6240\u6709\u7684\u53d8\u5316\u5728\u5176\u4ed6\u6570\u636e\u5e93\u8fde\u63a5\u4e0a\u90fd\u662f\u4e0d\u53ef\u89c1\u7684\u3002\u5982\u679c\u4f60\u5f80\u6570\u636e\u5e93\u91cc\u5199\u4e86\u6570\u636e\uff0c\u4f46\u662f\u53c8\u67e5\u8be2\u4e0d\u5230\uff0c\u8bf7\u68c0\u67e5\u662f\u5426\u5fd8\u8bb0\u4e86\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u662f\u6700\u53d7\u6b22\u8fce\u7684\u6570\u636e\u5e93\u4e4b\u4e00\uff0cPython \u9700\u8981 MySQL \u9a71\u52a8\u7a0b\u5e8f\u6765\u8bbf\u95ee MySQL \u6570\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45,59],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python","tag-sql"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}