python---连接MySQL第一页
2015-11-05 08:36
429 查看
前言:python如果想要连接到MySQL要安装上相关的驱动才下;好在驱动程序在mysql的官网可以下载的到。
以下是我自己保存的一个conetos7版本
http://yunpan.cn/cLACS7VurfaE4 访问密码 2e26
例子:
以下是我自己保存的一个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_编程特色
- python实现将内容分行输出
- 用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动
- 举例讲解Python中的死锁、可重入锁和互斥锁
- python 的列表遍历删除实现代码
- python基础
- python 数据类型与序列
- python入门犯错
- python datetime 转timestamp
- python datetime 转timestamp
- python正则表达式
- Python编程经验
- [python]pygame游戏开发学习(一)
- python 字符串操作
- python Pexpect
- 【数字的可视化:python画图之散点图sactter函数详解】
- 面向对象编程以及Python面向对象
- python模块之codecs: 自然语言编码转换
- Python爬虫实战(4):抓取淘宝MM照片
- python 画希尔伯特曲线