MySQLdb安装与使用

https://blog.csdn.net/u013107656/article/details/52245144

一、MAC系统

1. 安装(使用pip命令)

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

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

【3】要通过python连接mysql数据库,需要安装MySQLdb模块,该模块其实包含在MySQL-Python包中,所以使用pip安装时,需要搜索MySQL-Python

【4】python环境中输入 import MySQLdb ,如果不报错,表明安装成功

【5】安装过程中如果报错(mysql_config not found 或Reason: image not found)

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

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

参考链接: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

二、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文件,输出结果

三、python连接MySQL数据库 - PyMySQL

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

猜你喜欢

转载自blog.csdn.net/f110300641/article/details/81738225