Python版本:3.4.3
安装Lib库:pymysql
代码结构树:
主函数代码片dbcheck.py
#!/usr/bin/python # -*- coding: utf-8 -*- import configinit import pymysql def execute_mysql(): conn = None cur = None try: conn = pymysql.connect(**configinit._mysql_config) cur = conn.cursor() query = "SELECT * FROM publicnetbarpolicy;" cur.execute(query) result = cur.fetchall() for record in result: print(record) cur.close() conn.close() except pymysql.Error as e: print("Mysql Error %d: %s" % (e.args[0], e.args[1])) if __name__ == '__main__': execute_mysql()
数据库配置文件__init__.py
#!/usr/bin/python # -*- coding: utf-8 -*- _mysql_config = { 'user': 'root', 'password': 'js*%##))!!WX', 'host': '10.34.53.172', 'db': 'netbarapppolicys', 'charset':'utf8', }