码迷,mamicode.com
首页 > 数据库 > 详细

Python3.4连接Mysql

时间:2015-06-18 15:21:05      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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‘,
    }

 

Python3.4连接Mysql

标签:

原文地址:http://www.cnblogs.com/GoldbachRhapsody/p/4585716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!