{"id":17,"date":"2026-11-08T13:15:44","date_gmt":"2026-11-08T13:15:44","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=17"},"modified":"2026-11-08T13:15:44","modified_gmt":"2026-11-08T13:15:44","slug":"%e7%94%a8-python%ef%bc%88pymysql-sqlalchemy%ef%bc%89%e5%ae%89%e5%85%a8%e6%93%8d%e4%bd%9c%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=17","title":{"rendered":"\u7528 Python\uff08PyMySQL\/SQLAlchemy\uff09\u5b89\u5168\u64cd\u4f5c\u6570\u636e\u5e93"},"content":{"rendered":"<p>\u7528 Python \u8fde MySQL \u65f6\uff0c\u5b89\u5168\u7b2c\u4e00\u539f\u5219\u662f\uff1a<strong>\u6c38\u8fdc\u4e0d\u8981\u628a\u7528\u6237\u8f93\u5165\u62fc\u8fdb SQL \u5b57\u7b26\u4e32<\/strong>\u3002\u672c\u6587\u7ed9\u51fa PyMySQL \u4e0e SQLAlchemy \u7684\u6b63\u786e\u59ff\u52bf\u3002<\/p>\n<h2>PyMySQL \u53c2\u6570\u5316\u67e5\u8be2<\/h2>\n<pre><code>import pymysql\n\nconn = pymysql.connect(\n    host=\"127.0.0.1\",\n    user=\"app\",\n    password=\"***\",\n    database=\"demo\",\n    charset=\"utf8mb4\",\n    cursorclass=pymysql.cursors.DictCursor,\n)\n\ndef get_user(user_id: int):\n    sql = \"SELECT id, name, email FROM users WHERE id=%s\"\n    with conn.cursor() as cur:\n        cur.execute(sql, (user_id,))\n        return cur.fetchone()\n\n# \u9519\u8bef\u793a\u8303\uff1af-string \u62fc\u63a5\uff0c\u6709 SQL \u6ce8\u5165\u98ce\u9669\n# cur.execute(f\"SELECT * FROM users WHERE id={user_id}\")\n<\/code><\/pre>\n<h2>\u6279\u91cf\u63d2\u5165<\/h2>\n<pre><code>rows = [(\"Alice\", \"a@x.com\"), (\"Bob\", \"b@x.com\")]\nsql = \"INSERT INTO users(name, email) VALUES (%s, %s)\"\nwith conn.cursor() as cur:\n    cur.executemany(sql, rows)\nconn.commit()\n<\/code><\/pre>\n<h2>SQLAlchemy \u6838\u5fc3\u5199\u6cd5\uff08\u7b80\u7248\uff09<\/h2>\n<pre><code>from sqlalchemy import create_engine, text\n\nengine = create_engine(\"mysql+pymysql:\/\/app:***@127.0.0.1:3306\/demo?charset=utf8mb4\")\n\nwith engine.begin() as conn:\n    result = conn.execute(\n        text(\"SELECT * FROM users WHERE city=:city\"),\n        {\"city\": \"\u5317\u4eac\"},\n    )\n    for row in result.mappings():\n        print(row)\n<\/code><\/pre>\n<h2>\u5de5\u7a0b\u5b9e\u8df5\u6e05\u5355<\/h2>\n<ul>\n<li>\u8fde\u63a5\u4fe1\u606f\u653e\u73af\u5883\u53d8\u91cf\uff0c\u4e0d\u5199\u8fdb\u4ed3\u5e93<\/li>\n<li>\u751f\u4ea7\u5e93\u8d26\u53f7\u6700\u5c0f\u6743\u9650\uff08\u4e0d\u8981\u968f\u4fbf\u7528 root\uff09<\/li>\n<li>\u8bfb\u5199\u8d85\u65f6\u3001\u8fde\u63a5\u6c60\u3001\u5931\u8d25\u91cd\u8bd5\u8981\u6709<\/li>\n<li>\u53d8\u66f4\u6570\u636e\u5fc5\u987b\u4e8b\u52a1\uff1a\u6210\u529f <code>commit<\/code>\uff0c\u5931\u8d25 <code>rollback<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 Python \u8fde MySQL \u65f6\uff0c\u5b89\u5168\u7b2c\u4e00\u539f\u5219\u662f\uff1a\u6c38\u8fdc\u4e0d\u8981\u628a\u7528\u6237\u8f93\u5165\u62fc\u8fdb SQL \u5b57\u7b26\u4e32\u3002\u672c\u6587\u7ed9\u51fa PyM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}