请问一下,如何通过pycharm连接MySQL数据库,然后将数据显示在网页上
首先,这个与pycharm无关(它只是python开发工具),只要是python,任何开发工具都可以用以下简单的例子普实现你的功能
# -*- coding:utf-8 -*- import pymysql from ***.server import BaseHTTPRequestHandler, HTTPServer class RequestHandler(BaseHTTPRequestHandler): def do_GET(self): ConText=" <html> <body>
" db = ***.connect(host='localhost(服务器名)', user='你的用户名', password='口令', database='数据库名') cursor = db.cursor() cursor.execute("SELECT * from 表名") data = cursor.fetchall() db.close() ConText=ConText+str(data) ConText=ConText+"
</body> </html> " self.send_response(200) print(len(ConText)) self.send_header("Content-Type", "text/html") self.send_header("Content-Length", str(len(ConText))) self.end_headers() self.wfile.write(ConText.encode('utf-8')) if __name__ == '__main__': serverAddress = ('', 8080) server = HTTPServer(serverAddress, RequestHandler) server.serve_forever()必要条件
1。必须安装 pymysql包
2。你必须有个可以访问的mysql数据库
3。将上面的服务器名等内容替换成你实际的
如我的数据库
最终结果(输出格式自己调整)
***.connector类