Python MySQLdb Linux下安装笔记

 

       本文介绍了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版本。

  例:

import os,sys,string
import MySQLdb

try:
        conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',db='mysql')
except Exception,e:
        print(e)
        sys.exit('connect failed')

cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)                              #设置游标

sql = "select Host,User,Password from user"


try:
        ex = cur.execute(sql)
except Exception,e:
        print(e)

data = cur.fetchall()

cur.close()
conn.close()

print(ex)
print(data)


    三、python3.4源码包安装

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

     https://github.com/PyMySQL/PyMySQL


   下载解压后执行

                 python34 setup.py build

                 python34 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, ('[email protected]', 'very-secret'))

        cursor.excutemany                          #批量执行

    # 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, ('[email protected]',))
        result = cursor.fetchone()
        print(result)
finally:
    connection.close()

This example will print:

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


      四、要点说明:

      1、cursorclass=pymysql.cursors.DictCursor    返回结果以字典替换元祖

      2、fetchall             获取所有匹配数据

      3、fetchone           一条一条获取

      4、excutemany    批量操作

      5、scroll(-1,mode='relative')    相对位置,回到上一条

           scroll(0,mode='absolute')    绝对位置,回到记录第一条


  

     


你可能感兴趣的:(mysql,python)