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

Python MySQLdb Linux下安装笔记

时间:2015-06-20 22:12:00      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:python mysql

 

       本文介绍了Python MySQLdb Linux下安装笔记,本文分别讲解了快速安装和手动编译安装两种方法,并分别讲解了操作步骤,需要的朋友可以参考下

      主要针对centos6.5 64位系统

     

         默认python版本为2.6

         编码安装python2.7和python3.4


     一、yum快速安装

               yum install MySQL-python

               yum install python-setuptools 

      经常接触Python的同学可能会注意到,当需要安装第三方python包时,可能会用到easy_install命令。easy_install是由PEAK(Python Enterprise Application Kit)开发的setuptools包里带的一个命令,所以使用easy_install实际上是在调用setuptools来完成安装模块的工作。

Perl 用户比较熟悉 CPAN,而 Ruby 用户则比较熟悉 Gems;引导 setuptools 的 ez_setup 工具和随之而生的扩展后的 easy_install 与 “Cheeseshop”(Python Package Index,也称为 “PyPI”)一起工作来实现相同的功能。它可以很方便的让您自动下载,编译,安装和管理Python包。


     但yum安装的会默认安装到python2.6相应的目录下。


    二、在python2.7源码包安装

       1、需要:
                      A.gcc
                      B.setuptools 

            下载安装setuptools

                https://pypi.python.org/pypi/setuptools

            The recommended way to bootstrap setuptools on any system is to downloadez_setup.py and run it using the target Python environment. Different operating systems have different recommended techniques to accomplish this basic routine, so below are some examples to get you started.

           下载ez_setup.py 根据自己版本执行:

                 python27 ez_setup.py   读取python配置并下载setuptools-17.1.1.zip

           解压后执行:

                 python27 setup.py build

                 python27 setup.py install

          根据报错进行相应修改

      2、下载安装MySQLdb:

            下载http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

           解压后执行:

                 python27 setup.py build

                 python27 setup.py install

        注:此模块不支持python3.4版本。


    三、python3.4源码包安装

      在python3.4中使用原来python2.7的mysqldb已不能连接mysql数据库了,可以使用pymysql,来完成连接mysql的重任

     https://github.com/PyMySQL/PyMySQL


   下载解压后执行

                 python27 setup.py build

                 python27 setup.py install


    举例:


The following examples make use of a simple table

CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `email` varchar(255) COLLATE utf8_bin NOT NULL,
    `password` varchar(255) COLLATE utf8_bin NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
AUTO_INCREMENT=1 ;

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host=‘localhost‘,
                             user=‘user‘,
                             passwd=‘passwd‘,
                             db=‘db‘,
                             charset=‘utf8mb4‘,
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
        cursor.execute(sql, (‘webmaster@python.org‘, ‘very-secret‘))

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    connection.commit()

    with connection.cursor() as cursor:
        # Read a single record
        sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
        cursor.execute(sql, (‘webmaster@python.org‘,))
        result = cursor.fetchone()
        print(result)
finally:
    connection.close()

This example will print:

{‘password‘: ‘very-secret‘, ‘id‘: 1}






             


  

     


本文出自 “秋天的童话” 博客,请务必保留此出处http://wushank.blog.51cto.com/3489095/1663900

Python MySQLdb Linux下安装笔记

标签:python mysql

原文地址:http://wushank.blog.51cto.com/3489095/1663900

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