【MySQL链接】MySQLdb安装与使用

MySQLdb模块的安装与使用html


1、MAC系统python


1. 安装(使用pip命令)mysql


【1】使用 easy_install pip命令安装pipgit




【2】安装成功,输入pip显示用法、命令行等信息;命令 which pip 能够查看安装位置sql






【3】要经过python链接mysql数据库,须要安装MySQLdb模块,该模块其实包含在MySQL-Python包中,因此使用pip安装时,须要搜索MySQL-Python数据库




【4】python环境中输入 import MySQLdb ,若是不报错,代表安装成功centos




【5】安装过程当中若是报错(mysql_config not foundReason: image not found)测试


参考连接:http://www.cnblogs.com/ifantastic/archive/2013/04/13/3017677.htmlui


【6】手动安装方式 - 下载包,解压,执行命令spa


参考连接:http://www.jianshu.com/p/71cf187598ce


2. 使用方法


【1】PyCharm中 import MySQLdb,须要确保工程的Project Interpreter中包含MySQL-python模块




【2】py文件中使用MySQLdb.connect()方法,打开数据库链接




【3】关于connect()方法中,各个参数的含义


参考连接:https://moreoronce.gitbooks.io/learnpython/content/300/302.html


【4】若是语句中写成port=‘3306’,会报错(TypeError: an integer is required)


参考连接:http://stackoverflow.com/questions/15165577/typeerror-an-integer-is-required


【5】py文件中实现测试mysql数据库链接的代码




更多操做实例,参考连接:http://www.runoob.com/python/python-mysql.html


【6】关于python操做mysql数据库的更多内容


参考连接:

http://www.jianshu.com/p/76fab6cb06f9


http://www.jianshu.com/p/df19e4b86cfb



2、CentOS系统


1. 安装(使用yum命令)


【1】首先安装一些系统依赖包:gcc,python-devel,mysql-devel,zlib-devel,openssl-devel












参考连接:http://www.jianshu.com/p/76fab6cb06f9


【2】使用 yum -y install MySQL-python 命令安装MySQLdb模块




【3】安装成功,在python环境下验证,import MySQLdb,不会报错






【4】pip安装,会报错(command 'gcc' failed with exit status 1)






参考连接:http://blog.csdn.net/cryhelyxx/article/details/43604925


【5】下载源码包,执行命令安装


参考连接:http://54im.com/python/centos6-下安装mysql-python.html


2. 使用方法


【1】使用 mysql -u root -p 命令,登陆mysql数据库




【2】新建数据库cptest




关于mysql数据库操做的更多内容,参考连接:http://www.jianshu.com/p/5b29f4a5e0c0


【3】py文件,编写测试实例代码




【4】执行py文件,输出结果




3、python链接MySQL数据库 - PyMySQL


参考连接:http://www.jianshu.com/p/76fab6cb06f9