{"id":89993,"date":"2024-02-27T02:10:12","date_gmt":"2024-02-26T18:10:12","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=89993"},"modified":"2024-02-27T02:10:12","modified_gmt":"2024-02-26T18:10:12","slug":"python-connect-mysql","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-connect-mysql.html","title":{"rendered":"\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u7684\u65b9\u6cd5"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u5c3d\u7ba1\u5f88\u591a NoSQL \u6570\u636e\u5e93\u8fd1\u51e0\u5e74\u5927\u653e\u5f02\u5f69\uff0c\u4f46\u662f\u50cf MySQL \u8fd9\u6837\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4f9d\u7136\u662f\u4e92\u8054\u7f51\u7684\u4e3b\u6d41\u6570\u636e\u5e93\u4e4b\u4e00\uff0c\u6bcf\u4e2a\u5b66 Python \u7684\u90fd\u6709\u5fc5\u8981\u5b66\u597d\u4e00\u95e8\u6570\u636e\u5e93\uff0c\u4e0d\u7ba1\u4f60\u662f\u505a\u6570\u636e\u5206\u6790\uff0c\u8fd8\u662f\u7f51\u7edc\u722c\u866b\uff0cWeb \u5f00\u53d1\u3001\u4ea6\u6216\u662f\u673a\u5668\u5b66\u4e60\uff0c\u4f60\u90fd\u79bb\u4e0d\u5f00\u8981\u548c\u6570\u636e\u5e93\u6253\u4ea4\u9053\uff0c\u800c MySQL \u53c8\u662f\u6700\u6d41\u884c\u7684\u4e00\u79cd\u6570\u636e\u5e93\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd Python \u64cd\u4f5c MySQL \u7684\u51e0\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u5408\u7406\u9009\u62e9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n 1\u3001MySQL-python<\/strong><\/div>\n MySQL-python \u53c8\u53eb MySQLdb\uff0c\u662f Python \u8fde\u63a5 MySQL \u6700\u6d41\u884c\u7684\u4e00\u4e2a\u9a71\u52a8\uff0c\u5f88\u591a\u6846\u67b6\u90fd\u4e5f\u662f\u57fa\u4e8e\u6b64\u5e93\u8fdb\u884c\u5f00\u53d1\uff0c\u9057\u61be\u7684\u662f\u5b83\u53ea\u652f\u6301 Python2.x\uff0c\u800c\u4e14\u5b89\u88c5\u7684\u65f6\u5019\u6709\u5f88\u591a\u524d\u7f6e\u6761\u4ef6\uff0c\u56e0\u4e3a\u5b83\u662f\u57fa\u4e8eC\u5f00\u53d1\u7684\u5e93\uff0c\u5728 Windows \u5e73\u53f0\u5b89\u88c5\u975e\u5e38\u4e0d\u53cb\u597d\uff0c\u7ecf\u5e38\u51fa\u73b0\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u73b0\u5728\u57fa\u672c\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u53d6\u4ee3\u7684\u662f\u5b83\u7684\u884d\u751f\u7248\u672c\u3002<\/p>\n # \u524d\u7f6e\u6761\u4ef6sudo apt-get install python-dev libmysqlclient-dev \r\n# Ubuntusudo yum install python-devel mysql-devel \r\n# Red Hat \/ CentOS\r\n# \u5b89\u88c5pip install MySQL-python<\/pre>\n#!\/usr\/bin\/pythonimport MySQLdbdb = MySQLdb.connect( host=\"localhost\", \r\n# \u4e3b\u673a\u540d user=\"john\", \r\n# \u7528\u6237\u540d passwd=\"megajonhy\", \r\n# \u5bc6\u7801 db=\"jonhydb\") \r\n# \u6570\u636e\u5e93\u540d\u79f0\r\n# \u67e5\u8be2\u524d\uff0c\u5fc5\u987b\u5148\u83b7\u53d6\u6e38\u6807cur = db.cursor()\r\n# \u6267\u884c\u7684\u90fd\u662f\u539f\u751fSQL\u8bed\u53e5cur.execute(\"SELECT * FROM YOUR_TABLE_NAME\")for row in cur.fetchall(): print(row[0])db.close()<\/pre>\n |