mysqldb下载:

http://sourceforge.net/projects/mysql-python/

https://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/

http://mysql-python.sourceforge.net/



setuptools 工具安装:
1.wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py     //下载:ez_setup.py
2.python ez_setup.py --insecure
产生文件:easy_install,setuptools-23.1.0-py2.7.egg


    Installing easy_install script to /usr/local/bin

    Installing easy_install-2.7 script to /usr/local/bin

    Installed /usr/local/lib/python2.7/site-packages/setuptools-23.1.0-py2.7.egg     //记录结果

    [root@localhost ~]# which easy_install

    /usr/local/bin/easy_install



[root@localhost MySQL-python-1.2.3]# python setup.py install



[root@localhost site-packages]# ll
总用量 772
drwxr-xr-x 17 root root 4096 Jun 28 03:58 django
drwxr-xr-x 2 root root 4096 Jun 28 03:58 Django-1.9.7.dist-info
-rw-r--r-- 1 root root 291 Jun 28 04:04 easy-install.pth
drwxr-xr-x 3 root root 4096 Jun 28 03:39 MySQLdb
-rw-r--r-- 1 root root 2352 Jun 28 03:39 _mysql_exceptions.py
-rw-r--r-- 1 root root 4303 Jun 28 03:39 _mysql_exceptions.pyc
-rw-r--r-- 1 root root 105453 Jun 28 04:51 MySQL_python-1.2.3-py2.7-linux-x86_64.egg
drwxr-xr-x 2 root root 4096 Jun 28 03:39 MySQL_python-1.2.5-py2.7.egg-info
-rwxr-xr-x 1 root root 148389 Jun 28 03:39 _mysql.so
drwxr-xr-x 4 root root 4096 Jun 28 03:33 pip-1.5.4-py2.7.egg
-rw-r--r-- 1 root root 119 Mar 20 17:49 README
-rw-r--r-- 1 root root 472857 Jun 28 03:06 setuptools-23.1.0-py2.7.egg
-rw-r--r-- 1 root root 30 Jun 28 03:06 setuptools.pth
[root@localhost site-packages]# pwd
/usr/local/lib/python2.7/site-packages



pip, easy_install使用方式

安装使用easy_install
安装:
$ wget -q http://peak.telecommunity.com/dist/ez_setup.py
$ python ./ez_setup.py
使用:
$ easy_install PackageName #安装套件
$ easy_install -U PackageName #更新套件
$ easy_install -m PackageName #卸载套件
$ easy_install --showhelp #显示说明
安装使用pip
安装:
$ easy_install -U setuptools #更新setuptools版本(重要),pip依赖于setuptools
$ easy_install pip
使用:
$ pip install PackageName #安装套件
$ pip install -U PackageName #更新套件
$ pip uninstall PackageName #卸载套件
$ pip search PackageName #搜索套件
$ pip help #显示说明


 



#-*- encoding: gb2312 -*-
import os, sys, string
import MySQLdb

# 连接数据库 
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1')
except Exception, e:
print e
sys.exit()

# 获取cursor对象来进行操作

cursor = conn.cursor()
# 创建表
sql = "create table if not exists test1(name varchar(128) primary key, age int(4))"
cursor.execute(sql)
# 插入数据
sql = "insert into test1(name, age) values ('%s', %d)" % ("zhaowei", 23)
try:
cursor.execute(sql)
except Exception, e:
print e

sql = "insert into test1(name, age) values ('%s', %d)" % ("张三", 21)
try:
cursor.execute(sql)
except Exception, e:
print e
# 插入多条

sql = "insert into test1(name, age) values (%s, %s)"
val = (("李四", 24), ("王五", 25), ("洪六", 26))
try:
cursor.executemany(sql, val)
except Exception, e:
print e

#查询出数据
sql = "select * from test1"
cursor.execute(sql)
alldata = cursor.fetchall()
# 如果有数据返回,就循环输出, alldata是有个二维的列表
if alldata:
for rec in alldata:
print rec[0], rec[1]


cursor.close()

conn.close()