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

python---连接MySQL第一页

时间:2015-11-05 08:53:45      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

前言:python如果想要连接到MySQL要安装上相关的驱动才下;好在驱动程序在mysql的官网可以下载的到。

         以下是我自己保存的一个conetos7版本

         http://yunpan.cn/cLACS7VurfaE4  访问密码 2e26

例子:

#!/usr/bin/python
#!coding:utf-8

import mysql.connector
from mysql.connector import errorcode

def create_db(host=‘127.0.0.1‘,port=3306,user=‘admin‘,password=‘131417‘,database=‘studio‘):
    config={
    ‘host‘:host,
    ‘port‘:port,
    ‘user‘:user,
    ‘password‘:password,
    ‘database‘:database
    }
    try:
        conn=mysql.connector.connect(**config)
    except mysql.connector.Error,err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print ‘ACCESS_DENIED_ERROR something is worng your name or password‘
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print ‘database is not exists‘
        else:
            print err
    else:
        print ‘linked to database...‘
        cursor=conn.cursor()
        create_db=‘create database if not exists tempdb character set utf8;‘
        cursor.execute(create_db)
        print ‘database been created ...‘
        conn.close()

if __name__=="__main__":
    create_db()

 

python---连接MySQL第一页

标签:

原文地址:http://www.cnblogs.com/JiangLe/p/4938262.html

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