{"id":121197,"date":"2026-11-02T08:00:44","date_gmt":"2026-11-02T00:00:44","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/121197.html"},"modified":"2026-11-02T08:00:44","modified_gmt":"2026-11-02T00:00:44","slug":"mysql-%e5%a2%9e%e5%88%a0%e6%94%b9%e6%9f%a5%ef%bc%88crud%ef%bc%89%e8%af%a6%e8%a7%a3-python-pymysql%e5%ae%9e%e6%93%8d","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/121197.html","title":{"rendered":"MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd"},"content":{"rendered":"<h2>MySQL \u589e\u5220\u6539\u67e5&#xff08;CRUD&#xff09;\u8be6\u89e3 &#043; Python pymysql\u5b9e\u64cd<\/h2>\n<p>\u9002\u914dCSDN Markdown&#xff0c;\u76f4\u63a5\u5168\u9009\u590d\u5236\u7c98\u8d34\u5373\u53ef&#xff0c;\u65e0\u7279\u6b8a\u6837\u5f0f&#xff0c;\u4ee3\u7801\u5757\u3001\u8868\u683c\u6b63\u5e38\u8bc6\u522b<\/p>\n<h3>\u524d\u8a00<\/h3>\n<p>CRUD \u662f\u6570\u636e\u5e93\u6700\u6838\u5fc3\u7684\u56db\u4e2a\u64cd\u4f5c&#xff1a;\u65b0\u589e&#xff08;Create&#xff09;\u3001\u67e5\u8be2&#xff08;Read&#xff09;\u3001\u4fee\u6539&#xff08;Update&#xff09;\u3001\u5220\u9664&#xff08;Delete&#xff09;\u3002\u4e0d\u7ba1\u662f\u540e\u7aef\u5f00\u53d1\u3001\u671f\u672b\u6570\u636e\u5e93\u8003\u8bd5&#xff0c;\u8fd8\u662f\u5199 Python \u9879\u76ee&#xff0c;\u8fd9\u90fd\u662f\u57fa\u672c\u529f\u3002\u5f88\u591a\u65b0\u624b\u5bb9\u6613\u8e29 UPDATE\u3001DELETE \u5fd8\u8bb0 WHERE \u6761\u4ef6\u3001\u5fd8\u8bb0\u4e8b\u52a1\u63d0\u4ea4\u3001SQL \u6ce8\u5165\u8fd9\u4e9b\u5751&#xff0c;\u672c\u6587\u4ece\u539f\u751f SQL \u8bb2\u5230 Python \u4ee3\u7801\u5b9e\u64cd\u3002<\/p>\n<p>\u73af\u5883\u8bf4\u660e&#xff1a;MySQL 8.0&#xff0c;\u4f7f\u7528 pymysql \u5e93\u3002<\/p>\n<h3>\u4e00\u3001\u51c6\u5907\u6d4b\u8bd5\u8868<\/h3>\n<p>\u5148\u5efa\u4e00\u5f20\u5b66\u751f\u8868&#xff0c;\u540e\u9762\u6240\u6709 SQL \u90fd\u57fa\u4e8e\u8fd9\u5f20\u8868\u6d4b\u8bd5<\/p>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> student <span class=\"token punctuation\">(<\/span><br \/>\n    id <span class=\"token keyword\">INT<\/span> <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token keyword\">AUTO_INCREMENT<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u4e3b\u952eid&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    name <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u59d3\u540d&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    age <span class=\"token keyword\">TINYINT<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5e74\u9f84&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    score <span class=\"token keyword\">INT<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5206\u6570&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    is_delete <span class=\"token keyword\">TINYINT<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">0<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;0\u6b63\u5e38 1\u903b\u8f91\u5220\u9664&#039;<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">ENGINE<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">InnoDB<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CHARSET<\/span><span class=\"token operator\">&#061;<\/span>utf8mb4<span class=\"token punctuation\">;<\/span><\/p>\n<h3>\u4e8c\u3001\u67e5\u8be2 Read&#xff08;SELECT&#xff09;<\/h3>\n<p>\u67e5\u8be2\u662f\u4f7f\u7528\u6700\u591a\u7684\u64cd\u4f5c&#xff0c;\u8bfb\u53d6\u8868\u91cc\u9762\u7684\u6570\u636e\u3002<br \/>\n\u57fa\u7840\u8bed\u6cd5&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; \u67e5\u8be2\u5168\u90e8\u5b57\u6bb5<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> student<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u67e5\u8be2\u6307\u5b9a\u5b57\u6bb5&#xff08;\u63a8\u8350&#xff0c;\u4e0d\u8981\u76f4\u63a5\u7528*&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> id<span class=\"token punctuation\">,<\/span>name<span class=\"token punctuation\">,<\/span>score <span class=\"token keyword\">FROM<\/span> student<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5e26\u6761\u4ef6\u67e5\u8be2<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> student <span class=\"token keyword\">WHERE<\/span> age <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">18<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u6a21\u7cca\u67e5\u8be2<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> student <span class=\"token keyword\">WHERE<\/span> name <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;\u5f20%&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u6392\u5e8f order by<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> student <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> score <span class=\"token keyword\">DESC<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5206\u9875 limit<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> student <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5206\u7ec4 &#043; having<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> age<span class=\"token punctuation\">,<\/span><span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> num <span class=\"token keyword\">FROM<\/span> student <span class=\"token keyword\">GROUP<\/span> <span class=\"token keyword\">BY<\/span> age <span class=\"token keyword\">HAVING<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>&#x1f4a1; \u8e29\u5751\u70b9&#xff1a;WHERE \u8fc7\u6ee4\u539f\u59cb\u6570\u636e&#xff0c;HAVING \u8fc7\u6ee4\u5206\u7ec4\u4e4b\u540e\u7684\u7ed3\u679c&#xff1b;GROUP BY\u540e\u9762\u7684\u5b57\u6bb5&#xff0c;select\u91cc\u4e00\u822c\u53ea\u80fd\u653e\u5206\u7ec4\u5b57\u6bb5\u548c\u805a\u5408\u51fd\u6570\u3002<\/p>\n<h3>\u4e09\u3001\u65b0\u589e Create&#xff08;INSERT&#xff09;<\/h3>\n<p>\u5f80\u8868\u91cc\u63d2\u5165\u6570\u636e\u636e<\/p>\n<p><span class=\"token comment\">&#8212; \u5355\u6761\u63d2\u5165<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> student<span class=\"token punctuation\">(<\/span>name<span class=\"token punctuation\">,<\/span>age<span class=\"token punctuation\">,<\/span>score<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">VALUES<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u5f20\u4e09&#039;<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">19<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">88<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u6279\u91cf\u63d2\u5165&#xff08;\u6548\u7387\u66f4\u9ad8&#xff09;<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> student<span class=\"token punctuation\">(<\/span>name<span class=\"token punctuation\">,<\/span>age<span class=\"token punctuation\">,<\/span>score<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">VALUES<\/span><br \/>\n<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u674e\u56db&#039;<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">20<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">76<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u738b\u4e94&#039;<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">19<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">92<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token identifier\"><span class=\"token punctuation\">&#096;<\/span><span class=\"token punctuation\">&#096;<\/span><span class=\"token punctuation\">&#096;<\/span>&gt; &#x1f4a1; \u8e29\u5751\u70b9&#xff1a;\u4e3b\u952e\u8bbe\u7f6e AUTO_INCREMENT \u81ea\u589e&#xff0c;\u63d2\u5165\u7684\u65f6\u5019\u4e0d\u7528\u7ed9 id \u4f20\u503c&#xff1b;\u5b57\u7b26\u7c7b\u578b\u6570\u636e\u5fc5\u987b\u52a0\u5355\u5f15\u53f7\u3002\u53f7\u3002<\/p>\n<p>## \u56db\u3001\u4fee\u6539 Update&#xff08;UPDATE\u4fee\u6539\u5df2\u7ecf\u5b58\u5728\u7684\u6570\u636e\u3002\u6570\u636e<br \/>\n&#096;&#096;<span class=\"token punctuation\">&#096;<\/span><\/span><span class=\"token keyword\">sql<\/span><br \/>\n<span class=\"token comment\">&#8212; \u4fee\u6539\u5355\u4e2a\u5b57\u6bb5<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> student <span class=\"token keyword\">SET<\/span> score <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">90<\/span> <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4fee\u6539\u591a\u4e2a\u5b57UPDATE student SET age &#061; 20, score &#061; 85 WHERE name &#061; &#039;\u5f20\u4e09&#039;;&#039;;<\/span><\/p>\n<p>\u26a0\ufe0f\u3010\u91cd\u4e2d\u4e4b\u91cd\u3011UPDATE \u5982\u679c\u4e0d\u52a0 WHERE \u6761\u4ef6&#xff0c;\u6574\u5f20\u8868\u6240\u6709\u884c\u5168\u90e8\u88ab\u4fee\u6539&#xff0c;\u7ebf\u4e0a\u5e93\u5c5e\u4e8e\u91cd\u5927\u4e8b\u6545&#xff01;<br \/>\n\u5efa\u8bae\u4e60\u5148\u5199 SELECT \u6838\u5bf9 WHERE \u6761\u4ef6\u6761\u4ef6&#xff0c;\u786e\u8ba4\u662f\u8981\u6539\u7684\u6570\u518d\u6539\u6210 UPDATE\u3002E\u3002## \u4e94\u3001\u5220\u9664 Delete (DELETE)E&#xff09;<\/p>\n<p><span class=\"token comment\">&#8212; \u7269\u7406\u5220\u9664&#xff0c;\u5220\u6389\u8fd9\u4e00\u884c\u771f\u5b9e\u6570\u636e<\/span><br \/>\n<span class=\"token keyword\">DELETE<\/span> <span class=\"token keyword\">FROM<\/span> student <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h4>\u4e09\u4e2a\u5220\u9664\u8bed\u53e5\u5bf9\u6bd4&#xff08;\u8003\u8bd5\u9ad8\u9891\u8003\u70b9&#xff09;<\/h4>\n<p>|\u8bed\u53e5|\u4f5c\u7528|\u80fd\u4e0d\u80fd\u5e26where|\u81ea\u589e\u4e3b\u952e|\u4e8b\u52a1\u53ef\u56de\u6eda|<br \/>\n| &#8212;- | &#8212;- | &#8212;- | &#8212;- | &#8212;- | DELETE | \u5220\u9664\u884c\u6570\u636e | \u2705 \u652f\u6301 | \u4fdd\u7559&#xff0c;\u4e0d\u4f1a\u91cd\u7f6e | \u2705 \u53ef\u4ee5\u56de\u6eda |\u6eda| TRUNCATE | \u6e05\u7a7a\u6574\u5f20\u8868 | \u274c \u4e0d\u80fd | \u91cd\u7f6e\u4ece 1 \u5f00\u59cb | \u274c \u4e0d\u652f\u6301\u56de\u6eda |\u6eda| DROP | \u76f4\u63a5\u5220\u6389\u6574\u4e2a\u8868\u7ed3\u6784 | \u274c \u4e0d\u80fd | \u8868\u76f4\u63a5\u6d88\u5931 | \u274c \u4e0d\u652f\u6301\u56de\u6eda |\u6eda|&gt; &#x1f4a1; \u4e1a\u52a1\u5e38\u7528\u65b9\u6848&#xff1a;\u903b\u8f91\u5220\u9664&#xff08;\u8f6f\u5220\u9664&#xff09;&#xff0c;\u4e0d\u6267\u884c DELETE\u3002<\/p>\n<p>\u66f4\u65b0 is_delete&#061;1 \u4ee3\u8868\u5220\u9664&#xff0c;\u67e5\u8be2\u7684\u65f6\u5019\u5e26\u4e0a WHERE is_delete&#061;0&#061;0&#096;<\/p>\n<p><span class=\"token comment\">&#8212; \u903b\u8f91\u5220\u9664<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> student <span class=\"token keyword\">SET<\/span> is_delete<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1<\/span> <span class=\"token keyword\">WHERE<\/span> id<span class=\"token operator\">&#061;<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u67e5\u8be2\u6709\u6548\u6570\u636e<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> student <span class=\"token keyword\">WHERE<\/span> is_delete<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>\u516d\u3001Python pymysql \u8fde\u63a5\u6570\u636e\u5e93\u5b9e\u73b0CRUD<\/h3>\n<h4>1\u3001\u5148\u5b89\u88c5\u4f9d\u8d56<\/h4>\n<p>pip <span class=\"token function\">install<\/span> pymysql<br \/>\n<span class=\"token variable\"><span class=\"token variable\">&#096;<\/span><span class=\"token comment\">### 2\u3001\u5c01\u88c5\u5de5\u5177\u7c7b db.py&#xff08;\u8003\u8bd5 &#043; \u9879\u76ee\u901a\u7528\u6a21\u677f&#xff09;\u6a21\u677f&#xff09;<\/span><br \/>\n<span class=\"token variable\">&#096;<\/span><\/span>&#096;&#096;python<br \/>\n<span class=\"token function\">import<\/span> pymysql<\/p>\n<p>class MySQLHelper:<br \/>\n    def __init__<span class=\"token punctuation\">(<\/span>self, <span class=\"token assign-left variable\">host<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;localhost&#039;<\/span>, <span class=\"token assign-left variable\">user<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;root&#039;<\/span>, <span class=\"token assign-left variable\">password<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;123456&#039;<\/span>, <span class=\"token assign-left variable\">database<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;test_db&#039;<\/span><span class=\"token punctuation\">)<\/span>:<br \/>\n        <span class=\"token comment\"># \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/span><br \/>\n        self.conn <span class=\"token operator\">&#061;<\/span> pymysql.connect<span class=\"token punctuation\">(<\/span><br \/>\n            <span class=\"token assign-left variable\">host<\/span><span class=\"token operator\">&#061;<\/span>host,<br \/>\n            <span class=\"token assign-left variable\">user<\/span><span class=\"token operator\">&#061;<\/span>user,<br \/>\n            <span class=\"token assign-left variable\">password<\/span><span class=\"token operator\">&#061;<\/span>password,<br \/>\n            <span class=\"token assign-left variable\">database<\/span><span class=\"token operator\">&#061;<\/span>database,<br \/>\n            <span class=\"token assign-left variable\">charset<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;utf8mb4&#039;<\/span>,<br \/>\n            <span class=\"token assign-left variable\">cursorclass<\/span><span class=\"token operator\">&#061;<\/span>pymysql.cursors.DictCursor<br \/>\n        <span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># \u67e5\u8be2\u591a\u6761<\/span><br \/>\n    def query_all<span class=\"token punctuation\">(<\/span>self, sql, <span class=\"token assign-left variable\">params<\/span><span class=\"token operator\">&#061;<\/span>None<span class=\"token punctuation\">)<\/span>:<br \/>\n        params <span class=\"token operator\">&#061;<\/span> params or <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        with self.conn.cursor<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> as cur:<br \/>\n            cur.execute<span class=\"token punctuation\">(<\/span>sql, params<span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token builtin class-name\">return<\/span> cur.fetchall<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># \u67e5\u8be2\u5355\u6761<\/span><br \/>\n    def query_one<span class=\"token punctuation\">(<\/span>self, sql, <span class=\"token assign-left variable\">params<\/span><span class=\"token operator\">&#061;<\/span>None<span class=\"token punctuation\">)<\/span>:<br \/>\n        params <span class=\"token operator\">&#061;<\/span> params or <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        with self.conn.cursor<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> as cur:<br \/>\n            cur.execute<span class=\"token punctuation\">(<\/span>sql, params<span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token builtin class-name\">return<\/span> cur.fetchone<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># \u589e\u3001\u6539\u3001\u5220\u7edf\u4e00\u65b9\u6cd5<\/span><br \/>\n    def execute_sql<span class=\"token punctuation\">(<\/span>self, sql, <span class=\"token assign-left variable\">params<\/span><span class=\"token operator\">&#061;<\/span>None<span class=\"token punctuation\">)<\/span>:<br \/>\n        params <span class=\"token operator\">&#061;<\/span> params or <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        with self.conn.cursor<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> as cur:<br \/>\n            cur.execute<span class=\"token punctuation\">(<\/span>sql, params<span class=\"token punctuation\">)<\/span><br \/>\n            self.conn.commit<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># DML\u64cd\u4f5c\u5fc5\u987b\u63d0\u4ea4\u4e8b\u52a1<\/span><br \/>\n            <span class=\"token builtin class-name\">return<\/span> cur.rowcount<\/p>\n<p>    <span class=\"token comment\"># \u5173\u95ed\u8fde\u63a5<\/span><br \/>\n    def close<span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span>:<br \/>\n        self.conn.close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u6d4b\u8bd5\u8c03\u7528<\/span><br \/>\n<span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#039;__main__&#039;<\/span><span class=\"token builtin class-name\">:<\/span><br \/>\n    db <span class=\"token operator\">&#061;<\/span> MySQLHelper<span class=\"token punctuation\">(<\/span>password<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;\u4f60\u7684\u6570\u636e\u5e93\u5bc6\u7801&#034;<\/span>, <span class=\"token assign-left variable\">database<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;student&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># \u65b0\u589e<\/span><br \/>\n    <span class=\"token comment\"># sql &#061; &#034;INSERT INTO student(name,age,score) VALUES (%s,%s,%s)&#034;<\/span><br \/>\n    <span class=\"token comment\"># db.execute_sql(sql, (&#034;\u5c0f\u660e&#034;,18,80))<\/span><\/p>\n<p>    <span class=\"token comment\"># \u4fee\u6539<\/span><br \/>\n    <span class=\"token comment\"># sql &#061; &#034;UPDATE student SET score&#061;%s WHERE id&#061;%s&#034;<\/span><br \/>\n    <span class=\"token comment\"># db.execute_sql(sql, (86, 1))<\/span><\/p>\n<p>    <span class=\"token comment\"># \u5220\u9664<\/span><br \/>\n    <span class=\"token comment\"># sql &#061; &#034;DELETE FROM student WHERE id&#061;%s&#034;<\/span><br \/>\n    <span class=\"token comment\"># db.execute_sql(sql, (1,))<\/span><\/p>\n<p>    <span class=\"token comment\"># \u67e5\u8be2<\/span><br \/>\n    res <span class=\"token operator\">&#061;<\/span> db.query_all<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;SELECT * FROM student WHERE is_delete&#061;0&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    print<span class=\"token punctuation\">(<\/span>res<span class=\"token punctuation\">)<\/span><br \/>\n    db.close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>&#x1f4a1; &gt; 1. %s \u662f\u5360\u4f4d\u7b26&#xff0c;\u4e0d\u662f\u5b57\u7b26\u4e32\u62fc\u63a5&#xff01;\u7528\u6765\u9632\u6b62 SQL \u6ce8\u5165&#xff0c;\u7edd\u5bf9\u4e0d\u8981\u7528 f-string \u62fc\u63a5 SQL \u8bed\u53e5\u8bed\u53e5**<br \/>\n2. insert\/update\/delete \u6267\u884c\u5b8c\u5fc5\u987b commit() \u63d0\u4ea4\u4e8b\u52a1&#xff0c;\u5426\u5219\u6570\u636e\u5e93\u6570\u636e\u4e0d\u4f1a\u53d8\u5316<br \/>\n3. with \u7ba1\u7406\u6e38\u6807&#xff0c;\u81ea\u52a8\u5173\u95ed\u6e38\u6807&#xff0c;\u4e0d\u7528\u624b\u52a8\u5199 cur.close()<\/p>\n<h3>\u4e03\u3001\u9ad8\u9891\u66131. DQL&#xff08;select&#xff09;\u4e0d\u7528 commit&#xff1b;DML&#xff08;insert\/update\/delete&#xff09;\u9700\u8981 commitm2. update\u3001delete \u4e00\u5b9a\u8981\u52a0 where \u6761\u4ef6r3. \u6a21\u7cca\u67e5\u8be2 % \u5339\u914d\u4efb\u610f\u5b57\u7b26&#xff0c;_ \u5339\u914d\u5355\u4e2a\u5b57\u7b26\u53554. \u5916\u952e\u7ea6\u675f\u4fdd\u8bc1\u53c2\u7167\u5b8c\u6574\u6027&#xff0c;\u4e3b\u952e\u552f\u4e00\u975e\u7a7a&#xff0c;\u552f\u4e00\u7ea6\u675f\u5141\u8bb8\u4e00\u4e2a nulln5. \u4e8b\u52a1 ACID&#xff1a;\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u79bb\u6027\u3001\u6301\u4e45\u6027\u3001\u6301\u4e45\u6027<\/h3>\n<li>\u903b\u8f91\u5220\u9664\u9002\u5408\u4e1a\u52a1\u7cfb\u7edf&#xff0c;\u7269\u7406\u5220\u9664\u4e00\u822c\u7528\u4e8e\u6e05\u7406\u5783\u573e\u6570\u636e<\/li>\n<h3>\u516bCRUD \u770b\u7740\u7b80\u5355&#xff0c;\u4f46\u662f\u7ec6\u8282\u5751\u975e\u5e38\u591a\u3002\u719f\u7ec3\u638c\u63e1\u539f\u751f SQL&#xff0c;\u518d\u642d\u914d Python \u5c01\u88c5\u5de5\u5177\u7c7b&#xff0c;\u5de5\u5177\u7c7b&#xff0c;\u4e0d\u7ba1\u662f\u6570\u636e\u5e93\u671f\u672b\u8003\u8bd5\u3001\u540e\u7aef\u5f00\u53d1\u5199\u63a5\u53e3&#xff0c;\u90fd\u591f\u7528\u4e86\u3002\u540e\u7eed\u53ef\u4ee5\u7ee7\u7eed\u5b66\u4e60\u7d22\u5f15\u3001\u4e8b\u52a1\u3001\u9501\u3001\u8054\u8868\u67e5\u8be2\u8fd9\u4e9b\u8fdb\u9636\u5185\u5bb9\u3002<\/h3>\n<hr \/>\n<p>\u6807\u7b7e&#xff1a;MySQL CRUD pymysql \u6570\u636e\u5e93\u589e\u5220\u6539\u67e5 Python\u64cd\u4f5c\u6570\u636e\u5e93<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u589e\u5220\u6539\u67e5&#xff08;CRUD&#xff09;\u8be6\u89e3  Python pymysql\u5b9e\u64cd\u9002\u914dCSDN Markdown&#xff0c;\u76f4\u63a5\u5168\u9009\u590d\u5236\u7c98\u8d34\u5373\u53ef&#xff0c;\u65e0\u7279\u6b8a\u6837\u5f0f&#xff0c;\u4ee3\u7801\u5757\u3001\u8868\u683c\u6b63\u5e38\u8bc6\u522b\u524d\u8a00<br \/>\nCRUD \u662f\u6570\u636e\u5e93\u6700\u6838\u5fc3\u7684\u56db\u4e2a\u64cd\u4f5c&#xff1a;\u65b0\u589e&#xff08;Create&#xff09;\u3001\u67e5\u8be2&#xff08;Read&#xff09;\u3001\u4fee\u6539&#xff08;Update&#xff09;\u3001\u5220\u9664&#xff08;Delete&#xff09;\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[114,81,371,190,100],"topic":[],"class_list":["post-121197","post","type-post","status-publish","format-standard","hentry","category-server","tag-mysql","tag-python","tag-371","tag-190","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/121197.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"MySQL \u589e\u5220\u6539\u67e5&#xff08;CRUD&#xff09;\u8be6\u89e3 Python pymysql\u5b9e\u64cd\u9002\u914dCSDN Markdown&#xff0c;\u76f4\u63a5\u5168\u9009\u590d\u5236\u7c98\u8d34\u5373\u53ef&#xff0c;\u65e0\u7279\u6b8a\u6837\u5f0f&#xff0c;\u4ee3\u7801\u5757\u3001\u8868\u683c\u6b63\u5e38\u8bc6\u522b\u524d\u8a00 CRUD \u662f\u6570\u636e\u5e93\u6700\u6838\u5fc3\u7684\u56db\u4e2a\u64cd\u4f5c&#xff1a;\u65b0\u589e&#xff08;Create&#xff09;\u3001\u67e5\u8be2&#xff08;Read&#xff09;\u3001\u4fee\u6539&#xff08;Update&#xff09;\u3001\u5220\u9664&#xff08;Delete&#xff09;\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/121197.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-02T00:00:44+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/121197.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/121197.html\",\"name\":\"MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-02T00:00:44+00:00\",\"dateModified\":\"2026-11-02T00:00:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/121197.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/121197.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/121197.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/121197.html","og_locale":"zh_CN","og_type":"article","og_title":"MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"MySQL \u589e\u5220\u6539\u67e5&#xff08;CRUD&#xff09;\u8be6\u89e3 Python pymysql\u5b9e\u64cd\u9002\u914dCSDN Markdown&#xff0c;\u76f4\u63a5\u5168\u9009\u590d\u5236\u7c98\u8d34\u5373\u53ef&#xff0c;\u65e0\u7279\u6b8a\u6837\u5f0f&#xff0c;\u4ee3\u7801\u5757\u3001\u8868\u683c\u6b63\u5e38\u8bc6\u522b\u524d\u8a00 CRUD \u662f\u6570\u636e\u5e93\u6700\u6838\u5fc3\u7684\u56db\u4e2a\u64cd\u4f5c&#xff1a;\u65b0\u589e&#xff08;Create&#xff09;\u3001\u67e5\u8be2&#xff08;Read&#xff09;\u3001\u4fee\u6539&#xff08;Update&#xff09;\u3001\u5220\u9664&#xff08;Delete&#xff09;\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/121197.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-02T00:00:44+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/121197.html","url":"https:\/\/www.wsisp.com\/helps\/121197.html","name":"MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-02T00:00:44+00:00","dateModified":"2026-11-02T00:00:44+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/121197.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/121197.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/121197.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"MySQL \u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u8be6\u89e3 + Python pymysql\u5b9e\u64cd"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/121197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=121197"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/121197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=121197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=121197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=121197"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=121197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}