Python MySQLdb Linux下安裝筆記

來源:互聯網
上載者:User

標籤: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. Differentoperating systems have different recommended techniques to accomplish thisbasic 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, (‘[email protected]‘, ‘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, (‘[email protected]‘,))
        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下安裝筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.