{"id":167242,"date":"2020-01-01T12:03:41","date_gmt":"2020-01-01T04:03:41","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=167242"},"modified":"2019-12-23T11:04:40","modified_gmt":"2019-12-23T03:04:40","slug":"python3-mysql-database","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python3-mysql-database.html","title":{"rendered":"Python3 MySQL \u6570\u636e\u5e93\u8fde\u63a5 – PyMySQL \u9a71\u52a8"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPyMySQL \u662f\u5728 Python3.x \u7248\u672c\u4e2d\u7528\u4e8e\u8fde\u63a5 MySQL \u670d\u52a1\u5668\u7684\u4e00\u4e2a\u5e93\uff0cPython2\u4e2d\u5219\u4f7f\u7528mysqldb\u3002<\/p>\n

PyMySQL \u9075\u5faa Python \u6570\u636e\u5e93 API v2.0 \u89c4\u8303\uff0c\u5e76\u5305\u542b\u4e86 pure-Python MySQL \u5ba2\u6237\u7aef\u5e93\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

PyMySQL \u5b89\u88c5<\/strong><\/div>\n

\u5728\u4f7f\u7528 PyMySQL \u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd PyMySQL \u5df2\u5b89\u88c5\u3002<\/p>\n

PyMySQL \u4e0b\u8f7d\u5730\u5740\uff1ahttps:\/\/github.com\/PyMySQL\/PyMySQL\u3002<\/p>\n

\u5982\u679c\u8fd8\u672a\u5b89\u88c5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u6700\u65b0\u7248\u7684 PyMySQL\uff1a<\/p>\n

$ pip3 install PyMySQL<\/pre>\n

\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u4e0d\u652f\u6301 pip \u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u5b89\u88c5\uff1a<\/p>\n

1\u3001\u4f7f\u7528 git \u547d\u4ee4\u4e0b\u8f7d\u5b89\u88c5\u5305\u5b89\u88c5(\u4f60\u4e5f\u53ef\u4ee5\u624b\u52a8\u4e0b\u8f7d)\uff1a<\/p>\n

$ git clone https:\/\/github.com\/PyMySQL\/PyMySQL\r\n$ cd PyMySQL\/\r\n$ python3 setup.py install<\/pre>\n

2\u3001\u5982\u679c\u9700\u8981\u5236\u5b9a\u7248\u672c\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528 curl \u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n

$ # X.X \u4e3a PyMySQL \u7684\u7248\u672c\u53f7\r\n$ curl -L https:\/\/github.com\/PyMySQL\/PyMySQL\/tarball\/pymysql-X.X | tar xz\r\n$ cd PyMySQL*\r\n$ python3 setup.py install\r\n$ # \u73b0\u5728\u4f60\u53ef\u4ee5\u5220\u9664 PyMySQL* \u76ee\u5f55<\/pre>\n

\u6ce8\u610f\uff1a\u8bf7\u786e\u4fdd\u60a8\u6709root\u6743\u9650\u6765\u5b89\u88c5\u4e0a\u8ff0\u6a21\u5757\u3002<\/p>\n

\u5b89\u88c5\u7684\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\"ImportError: No module named setuptools\"\u7684\u9519\u8bef\u63d0\u793a\uff0c\u610f\u601d\u662f\u4f60\u6ca1\u6709\u5b89\u88c5setuptools\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95eehttps:\/\/pypi.python.org\/pypi\/setuptools \u627e\u5230\u5404\u4e2a\u7cfb\u7edf\u7684\u5b89\u88c5\u65b9\u6cd5\u3002<\/p>\n

Linux \u7cfb\u7edf\u5b89\u88c5\u5b9e\u4f8b\uff1a<\/p>\n

$ wget https:\/\/bootstrap.pypa.io\/ez_setup.py\r\n$ python3 ez_setup.py<\/pre>\n
\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/div>\n

\u8fde\u63a5\u6570\u636e\u5e93\u524d\uff0c\u8bf7\u5148\u786e\u8ba4\u4ee5\u4e0b\u4e8b\u9879\uff1a<\/p>\n

\u60a8\u5df2\u7ecf\u521b\u5efa\u4e86\u6570\u636e\u5e93 TESTDB.
\n\u5728TESTDB\u6570\u636e\u5e93\u4e2d\u60a8\u5df2\u7ecf\u521b\u5efa\u4e86\u8868 EMPLOYEE
\nEMPLOYEE\u8868\u5b57\u6bb5\u4e3a FIRST_NAME, LAST_NAME, AGE, SEX \u548c INCOME\u3002
\n\u8fde\u63a5\u6570\u636e\u5e93TESTDB\u4f7f\u7528\u7684\u7528\u6237\u540d\u4e3a \"testuser\" \uff0c\u5bc6\u7801\u4e3a \"test123\",\u4f60\u53ef\u4ee5\u53ef\u4ee5\u81ea\u5df1\u8bbe\u5b9a\u6216\u8005\u76f4\u63a5\u4f7f\u7528root\u7528\u6237\u540d\u53ca\u5176\u5bc6\u7801\uff0cMysql\u6570\u636e\u5e93\u7528\u6237\u6388\u6743\u8bf7\u4f7f\u7528Grant\u547d\u4ee4\u3002
\n\u5728\u4f60\u7684\u673a\u5b50\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86 Python MySQLdb \u6a21\u5757\u3002
\n\u5982\u679c\u60a8\u5bf9sql\u8bed\u53e5\u4e0d\u719f\u6089\uff0c\u53ef\u4ee5\u8bbf\u95ee\u6211\u4eec\u7684 SQL\u57fa\u7840\u6559\u7a0b
\n\u5b9e\u4f8b\uff1a
\n\u4ee5\u4e0b\u5b9e\u4f8b\u94fe\u63a5 Mysql \u7684 TESTDB \u6570\u636e\u5e93\uff1a<\/p>\n

\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nimport pymysql\r\n \r\n# \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\r\ndb = pymysql.connect(\"localhost\",\"testuser\",\"test123\",\"TESTDB\" )\r\n \r\n# \u4f7f\u7528 cursor() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61 cursor\r\ncursor = db.cursor()\r\n \r\n# \u4f7f\u7528 execute()  \u65b9\u6cd5\u6267\u884c SQL \u67e5\u8be2 \r\ncursor.execute(\"SELECT VERSION()\")\r\n \r\n# \u4f7f\u7528 fetchone() \u65b9\u6cd5\u83b7\u53d6\u5355\u6761\u6570\u636e.\r\ndata = cursor.fetchone()\r\n \r\nprint (\"Database version : %s \" % data)\r\n \r\n# \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\ndb.close()<\/pre>\n

\u6267\u884c\u4ee5\u4e0a\u811a\u672c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

Database version : 5.5.20-log<\/pre>\n
\u521b\u5efa\u6570\u636e\u5e93\u8868<\/strong><\/div>\n

\u5982\u679c\u6570\u636e\u5e93\u8fde\u63a5\u5b58\u5728\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528execute()\u65b9\u6cd5\u6765\u4e3a\u6570\u636e\u5e93\u521b\u5efa\u8868\uff0c\u5982\u4e0b\u6240\u793a\u521b\u5efa\u8868EMPLOYEE\uff1a<\/p>\n

\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nimport pymysql\r\n \r\n# \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\r\ndb = pymysql.connect(\"localhost\",\"testuser\",\"test123\",\"TESTDB\" )\r\n \r\n# \u4f7f\u7528 cursor() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61 cursor\r\ncursor = db.cursor()\r\n \r\n# \u4f7f\u7528 execute() \u65b9\u6cd5\u6267\u884c SQL\uff0c\u5982\u679c\u8868\u5b58\u5728\u5219\u5220\u9664\r\ncursor.execute(\"DROP TABLE IF EXISTS EMPLOYEE\")\r\n \r\n# \u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u521b\u5efa\u8868\r\nsql = \"\"\"CREATE TABLE EMPLOYEE (\r\n         FIRST_NAME  CHAR(20) NOT NULL,\r\n         LAST_NAME  CHAR(20),\r\n         AGE INT,  \r\n         SEX CHAR(1),\r\n         INCOME FLOAT )\"\"\"\r\n \r\ncursor.execute(sql)\r\n \r\n# \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\ndb.close()<\/pre>\n
\u6570\u636e\u5e93\u63d2\u5165\u64cd\u4f5c<\/strong><\/div>\n

\u4ee5\u4e0b\u5b9e\u4f8b\u4f7f\u7528\u6267\u884c SQL INSERT \u8bed\u53e5\u5411\u8868 EMPLOYEE \u63d2\u5165\u8bb0\u5f55\uff1a<\/p>\n

\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nimport pymysql\r\n \r\n# \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\r\ndb = pymysql.connect(\"localhost\",\"testuser\",\"test123\",\"TESTDB\" )\r\n \r\n# \u4f7f\u7528cursor()\u65b9\u6cd5\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807 \r\ncursor = db.cursor()\r\n \r\n# SQL \u63d2\u5165\u8bed\u53e5\r\nsql = \"\"\"INSERT INTO EMPLOYEE(FIRST_NAME,\r\n         LAST_NAME, AGE, SEX, INCOME)\r\n         VALUES ('Mac', 'Mohan', 20, 'M', 2000)\"\"\"\r\ntry:\r\n   # \u6267\u884csql\u8bed\u53e5\r\n   cursor.execute(sql)\r\n   # \u63d0\u4ea4\u5230\u6570\u636e\u5e93\u6267\u884c\r\n   db.commit()\r\nexcept:\r\n   # \u5982\u679c\u53d1\u751f\u9519\u8bef\u5219\u56de\u6eda\r\n   db.rollback()\r\n \r\n# \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\ndb.close()<\/pre>\n

\u4ee5\u4e0a\u4f8b\u5b50\u4e5f\u53ef\u4ee5\u5199\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n

\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nimport pymysql\r\n \r\n# \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\r\ndb = pymysql.connect(\"localhost\",\"testuser\",\"test123\",\"TESTDB\" )\r\n \r\n# \u4f7f\u7528cursor()\u65b9\u6cd5\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807 \r\ncursor = db.cursor()\r\n \r\n# SQL \u63d2\u5165\u8bed\u53e5\r\nsql = \"INSERT INTO EMPLOYEE(FIRST_NAME, \\\r\n       LAST_NAME, AGE, SEX, INCOME) \\\r\n       VALUES ('%s', '%s',  %s,  '%s',  %s)\" % \\\r\n       ('Mac', 'Mohan', 20, 'M', 2000)\r\ntry:\r\n   # \u6267\u884csql\u8bed\u53e5\r\n   cursor.execute(sql)\r\n   # \u6267\u884csql\u8bed\u53e5\r\n   db.commit()\r\nexcept:\r\n   # \u53d1\u751f\u9519\u8bef\u65f6\u56de\u6eda\r\n   db.rollback()\r\n \r\n# \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\ndb.close()<\/pre>\n

\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528\u53d8\u91cf\u5411SQL\u8bed\u53e5\u4e2d\u4f20\u9012\u53c2\u6570:<\/p>\n

..................................\r\nuser_id = \"test123\"\r\npassword = \"password\"\r\n\r\ncon.execute('insert into Login values( %s,  %s)' % \\\r\n             (user_id, password))\r\n..................................<\/pre>\n
\u6570\u636e\u5e93\u67e5\u8be2\u64cd\u4f5c<\/strong><\/div>\n

Python\u67e5\u8be2Mysql\u4f7f\u7528 fetchone() \u65b9\u6cd5\u83b7\u53d6\u5355\u6761\u6570\u636e, \u4f7f\u7528fetchall() \u65b9\u6cd5\u83b7\u53d6\u591a\u6761\u6570\u636e\u3002<\/p>\n

fetchone(): \u8be5\u65b9\u6cd5\u83b7\u53d6\u4e0b\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\u96c6\u3002\u7ed3\u679c\u96c6\u662f\u4e00\u4e2a\u5bf9\u8c61
\nfetchall(): \u63a5\u6536\u5168\u90e8\u7684\u8fd4\u56de\u7ed3\u679c\u884c.
\nrowcount: \u8fd9\u662f\u4e00\u4e2a\u53ea\u8bfb\u5c5e\u6027\uff0c\u5e76\u8fd4\u56de\u6267\u884cexecute()\u65b9\u6cd5\u540e\u5f71\u54cd\u7684\u884c\u6570\u3002
\n\u5b9e\u4f8b\uff1a
\n\u67e5\u8be2EMPLOYEE\u8868\u4e2dsalary\uff08\u5de5\u8d44\uff09\u5b57\u6bb5\u5927\u4e8e1000\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n

\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nimport pymysql\r\n \r\n# \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\r\ndb = pymysql.connect(\"localhost\",\"testuser\",\"test123\",\"TESTDB\" )\r\n \r\n# \u4f7f\u7528cursor()\u65b9\u6cd5\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807 \r\ncursor = db.cursor()\r\n \r\n# SQL \u67e5\u8be2\u8bed\u53e5\r\nsql = \"SELECT * FROM EMPLOYEE \\\r\n       WHERE INCOME > %s\" % (1000)\r\ntry:\r\n   # \u6267\u884cSQL\u8bed\u53e5\r\n   cursor.execute(sql)\r\n   # \u83b7\u53d6\u6240\u6709\u8bb0\u5f55\u5217\u8868\r\n   results = cursor.fetchall()\r\n   for row in results:\r\n      fname = row[0]\r\n      lname = row[1]\r\n      age = row[2]\r\n      sex = row[3]\r\n      income = row[4]\r\n       # \u6253\u5370\u7ed3\u679c\r\n      print (\"fname=%s,lname=%s,age=%s,sex=%s,income=%s\" % \\\r\n             (fname, lname, age, sex, income ))\r\nexcept:\r\n   print (\"Error: unable to fetch data\")\r\n \r\n# \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\ndb.close()<\/pre>\n

\u4ee5\u4e0a\u811a\u672c\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

fname=Mac, lname=Mohan, age=20, sex=M, income=2000<\/pre>\n
\u6570\u636e\u5e93\u66f4\u65b0\u64cd\u4f5c<\/strong><\/div>\n

\u66f4\u65b0\u64cd\u4f5c\u7528\u4e8e\u66f4\u65b0\u6570\u636e\u8868\u7684\u7684\u6570\u636e\uff0c\u4ee5\u4e0b\u5b9e\u4f8b\u5c06 TESTDB \u8868\u4e2d SEX \u4e3a 'M' \u7684 AGE \u5b57\u6bb5\u9012\u589e 1\uff1a<\/p>\n

\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nimport pymysql\r\n \r\n# \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\r\ndb = pymysql.connect(\"localhost\",\"testuser\",\"test123\",\"TESTDB\" )\r\n \r\n# \u4f7f\u7528cursor()\u65b9\u6cd5\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807 \r\ncursor = db.cursor()\r\n \r\n# SQL \u66f4\u65b0\u8bed\u53e5\r\nsql = \"UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'\" % ('M')\r\ntry:\r\n   # \u6267\u884cSQL\u8bed\u53e5\r\n   cursor.execute(sql)\r\n   # \u63d0\u4ea4\u5230\u6570\u636e\u5e93\u6267\u884c\r\n   db.commit()\r\nexcept:\r\n   # \u53d1\u751f\u9519\u8bef\u65f6\u56de\u6eda\r\n   db.rollback()\r\n \r\n# \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\ndb.close()<\/pre>\n
\u5220\u9664\u64cd\u4f5c<\/strong><\/div>\n

\u5220\u9664\u64cd\u4f5c\u7528\u4e8e\u5220\u9664\u6570\u636e\u8868\u4e2d\u7684\u6570\u636e\uff0c\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a\u4e86\u5220\u9664\u6570\u636e\u8868 EMPLOYEE \u4e2d AGE \u5927\u4e8e 20 \u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n

\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nimport pymysql\r\n \r\n# \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\r\ndb = pymysql.connect(\"localhost\",\"testuser\",\"test123\",\"TESTDB\" )\r\n \r\n# \u4f7f\u7528cursor()\u65b9\u6cd5\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807 \r\ncursor = db.cursor()\r\n \r\n# SQL \u5220\u9664\u8bed\u53e5\r\nsql = \"DELETE FROM EMPLOYEE WHERE AGE > %s\" % (20)\r\ntry:\r\n   # \u6267\u884cSQL\u8bed\u53e5\r\n   cursor.execute(sql)\r\n   # \u63d0\u4ea4\u4fee\u6539\r\n   db.commit()\r\nexcept:\r\n   # \u53d1\u751f\u9519\u8bef\u65f6\u56de\u6eda\r\n   db.rollback()\r\n \r\n# \u5173\u95ed\u8fde\u63a5\r\ndb.close()<\/pre>\n
\u6267\u884c\u4e8b\u52a1<\/strong><\/div>\n

\u4e8b\u52a1\u673a\u5236\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u3002<\/p>\n

\u4e8b\u52a1\u5e94\u8be5\u5177\u67094\u4e2a\u5c5e\u6027\uff1a\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u79bb\u6027\u3001\u6301\u4e45\u6027\u3002\u8fd9\u56db\u4e2a\u5c5e\u6027\u901a\u5e38\u79f0\u4e3aACID\u7279\u6027\u3002<\/p>\n

\u539f\u5b50\u6027\uff08atomicity\uff09\u3002\u4e00\u4e2a\u4e8b\u52a1\u662f\u4e00\u4e2a\u4e0d\u53ef\u5206\u5272\u7684\u5de5\u4f5c\u5355\u4f4d\uff0c\u4e8b\u52a1\u4e2d\u5305\u62ec\u7684\u8bf8\u64cd\u4f5c\u8981\u4e48\u90fd\u505a\uff0c\u8981\u4e48\u90fd\u4e0d\u505a\u3002
\n\u4e00\u81f4\u6027\uff08consistency\uff09\u3002\u4e8b\u52a1\u5fc5\u987b\u662f\u4f7f\u6570\u636e\u5e93\u4ece\u4e00\u4e2a\u4e00\u81f4\u6027\u72b6\u6001\u53d8\u5230\u53e6\u4e00\u4e2a\u4e00\u81f4\u6027\u72b6\u6001\u3002\u4e00\u81f4\u6027\u4e0e\u539f\u5b50\u6027\u662f\u5bc6\u5207\u76f8\u5173\u7684\u3002
\n\u9694\u79bb\u6027\uff08isolation\uff09\u3002\u4e00\u4e2a\u4e8b\u52a1\u7684\u6267\u884c\u4e0d\u80fd\u88ab\u5176\u4ed6\u4e8b\u52a1\u5e72\u6270\u3002\u5373\u4e00\u4e2a\u4e8b\u52a1\u5185\u90e8\u7684\u64cd\u4f5c\u53ca\u4f7f\u7528\u7684\u6570\u636e\u5bf9\u5e76\u53d1\u7684\u5176\u4ed6\u4e8b\u52a1\u662f\u9694\u79bb\u7684\uff0c\u5e76\u53d1\u6267\u884c\u7684\u5404\u4e2a\u4e8b\u52a1\u4e4b\u95f4\u4e0d\u80fd\u4e92\u76f8\u5e72\u6270\u3002
\n\u6301\u4e45\u6027\uff08durability\uff09\u3002\u6301\u7eed\u6027\u4e5f\u79f0\u6c38\u4e45\u6027\uff08permanence\uff09\uff0c\u6307\u4e00\u4e2a\u4e8b\u52a1\u4e00\u65e6\u63d0\u4ea4\uff0c\u5b83\u5bf9\u6570\u636e\u5e93\u4e2d\u6570\u636e\u7684\u6539\u53d8\u5c31\u5e94\u8be5\u662f\u6c38\u4e45\u6027\u7684\u3002\u63a5\u4e0b\u6765\u7684\u5176\u4ed6\u64cd\u4f5c\u6216\u6545\u969c\u4e0d\u5e94\u8be5\u5bf9\u5176\u6709\u4efb\u4f55\u5f71\u54cd\u3002
\nPython DB API 2.0 \u7684\u4e8b\u52a1\u63d0\u4f9b\u4e86\u4e24\u4e2a\u65b9\u6cd5 commit \u6216 rollback\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

\u5b9e\u4f8b(Python 3.0+)\r\n# SQL\u5220\u9664\u8bb0\u5f55\u8bed\u53e5\r\nsql = \"DELETE FROM EMPLOYEE WHERE AGE > %s\" % (20)\r\ntry:\r\n   # \u6267\u884cSQL\u8bed\u53e5\r\n   cursor.execute(sql)\r\n   # \u5411\u6570\u636e\u5e93\u63d0\u4ea4\r\n   db.commit()\r\nexcept:\r\n   # \u53d1\u751f\u9519\u8bef\u65f6\u56de\u6eda\r\n   db.rollback()<\/pre>\n

\u5bf9\u4e8e\u652f\u6301\u4e8b\u52a1\u7684\u6570\u636e\u5e93\uff0c \u5728Python\u6570\u636e\u5e93\u7f16\u7a0b\u4e2d\uff0c\u5f53\u6e38\u6807\u5efa\u7acb\u4e4b\u65f6\uff0c\u5c31\u81ea\u52a8\u5f00\u59cb\u4e86\u4e00\u4e2a\u9690\u5f62\u7684\u6570\u636e\u5e93\u4e8b\u52a1\u3002<\/p>\n

commit()\u65b9\u6cd5\u6e38\u6807\u7684\u6240\u6709\u66f4\u65b0\u64cd\u4f5c\uff0crollback\uff08\uff09\u65b9\u6cd5\u56de\u6eda\u5f53\u524d\u6e38\u6807\u7684\u6240\u6709\u64cd\u4f5c\u3002\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u90fd\u5f00\u59cb\u4e86\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002<\/p>\n

\u9519\u8bef\u5904\u7406<\/strong><\/div>\n

DB API\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e9b\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u9519\u8bef\u53ca\u5f02\u5e38\uff0c\u4e0b\u8868\u5217\u51fa\u4e86\u8fd9\u4e9b\u9519\u8bef\u548c\u5f02\u5e38:<\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\u5f02\u5e38<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n
Warning<\/td>\n\u5f53\u6709\u4e25\u91cd\u8b66\u544a\u65f6\u89e6\u53d1\uff0c\u4f8b\u5982\u63d2\u5165\u6570\u636e\u662f\u88ab\u622a\u65ad\u7b49\u7b49\u3002\u5fc5\u987b\u662f StandardError \u7684\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
Error<\/td>\n\u8b66\u544a\u4ee5\u5916\u6240\u6709\u5176\u4ed6\u9519\u8bef\u7c7b\u3002\u5fc5\u987b\u662f StandardError \u7684\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
InterfaceError<\/td>\n\u5f53\u6709\u6570\u636e\u5e93\u63a5\u53e3\u6a21\u5757\u672c\u8eab\u7684\u9519\u8bef\uff08\u800c\u4e0d\u662f\u6570\u636e\u5e93\u7684\u9519\u8bef\uff09\u53d1\u751f\u65f6\u89e6\u53d1\u3002 \u5fc5\u987b\u662fError\u7684\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
DatabaseError<\/td>\n\u548c\u6570\u636e\u5e93\u6709\u5173\u7684\u9519\u8bef\u53d1\u751f\u65f6\u89e6\u53d1\u3002 \u5fc5\u987b\u662fError\u7684\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
DataError<\/td>\n\u5f53\u6709\u6570\u636e\u5904\u7406\u65f6\u7684\u9519\u8bef\u53d1\u751f\u65f6\u89e6\u53d1\uff0c\u4f8b\u5982\uff1a\u9664\u96f6\u9519\u8bef\uff0c\u6570\u636e\u8d85\u8303\u56f4\u7b49\u7b49\u3002 \u5fc5\u987b\u662fDatabaseError\u7684\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
OperationalError<\/td>\n\u6307\u975e\u7528\u6237\u63a7\u5236\u7684\uff0c\u800c\u662f\u64cd\u4f5c\u6570\u636e\u5e93\u65f6\u53d1\u751f\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff1a\u8fde\u63a5\u610f\u5916\u65ad\u5f00\u3001 \u6570\u636e\u5e93\u540d\u672a\u627e\u5230\u3001\u4e8b\u52a1\u5904\u7406\u5931\u8d25\u3001\u5185\u5b58\u5206\u914d\u9519\u8bef\u7b49\u7b49\u64cd\u4f5c\u6570\u636e\u5e93\u662f\u53d1\u751f\u7684\u9519\u8bef\u3002 \u5fc5\u987b\u662fDatabaseError\u7684\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
IntegrityError<\/td>\n\u5b8c\u6574\u6027\u76f8\u5173\u7684\u9519\u8bef\uff0c\u4f8b\u5982\u5916\u952e\u68c0\u67e5\u5931\u8d25\u7b49\u3002\u5fc5\u987b\u662fDatabaseError\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
InternalError<\/td>\n \u6570\u636e\u5e93\u7684\u5185\u90e8\u9519\u8bef\uff0c\u4f8b\u5982\u6e38\u6807\uff08cursor\uff09\u5931\u6548\u4e86\u3001\u4e8b\u52a1\u540c\u6b65\u5931\u8d25\u7b49\u7b49\u3002 \u5fc5\u987b\u662fDatabaseError\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
ProgrammingError<\/td>\n\u7a0b\u5e8f\u9519\u8bef\uff0c\u4f8b\u5982\u6570\u636e\u8868\uff08table\uff09\u6ca1\u627e\u5230\u6216\u5df2\u5b58\u5728\u3001SQL\u8bed\u53e5\u8bed\u6cd5\u9519\u8bef\u3001 \u53c2\u6570\u6570\u91cf\u9519\u8bef\u7b49\u7b49\u3002\u5fc5\u987b\u662fDatabaseError\u7684\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n
NotSupportedError<\/td>\n\u4e0d\u652f\u6301\u9519\u8bef\uff0c\u6307\u4f7f\u7528\u4e86\u6570\u636e\u5e93\u4e0d\u652f\u6301\u7684\u51fd\u6570\u6216API\u7b49\u3002\u4f8b\u5982\u5728\u8fde\u63a5\u5bf9\u8c61\u4e0a \u4f7f\u7528.rollback()\u51fd\u6570\uff0c\u7136\u800c\u6570\u636e\u5e93\u5e76\u4e0d\u652f\u6301\u4e8b\u52a1\u6216\u8005\u4e8b\u52a1\u5df2\u5173\u95ed\u3002 \u5fc5\u987b\u662fDatabaseError\u7684\u5b50\u7c7b\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"

\u5728\u4f7f\u7528 PyMySQL \u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd PyMySQL \u5df2\u5b89\u88c5\u3002 PyMySQL \u4e0b\u8f7d\u5730\u5740\uff1ahttps:\/ […]<\/p>\n","protected":false},"author":1470,"featured_media":167244,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167242","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167242"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167242\/revisions"}],"predecessor-version":[{"id":167500,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167242\/revisions\/167500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167244"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}