(1)pyhton shell下导入MySQLdb失败。
http://pypi.python.org/pypi/MySQL-python/(2)下载解压MySQL-python。http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.3.tar.gz安装: python setup.py build && python setup.py install
buid出错缺少setuptools,如下:
Traceback (most recent call last): File "setup.py", line 5, in <module> from setuptools import setup, ExtensionImportError: No module named setuptools(4)下载安装setuptools。http://pypi.python.org/pypi/setuptools #downloads 选自己Python对应的版本。python setup.py build && python setup.py install
root@192.168.1.104:~/software/python# pythonPython 2.6.6 (r266:84292, Jun 18 2012, 14:10:23)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import setuptools上面没有报错说明安装setuptools成功了的。(5)安装 mysql-devel 及其他必要的库yum install mysql mysql-devel(6)build mysqldbcd MySQL-python-1.2.3/MySQL-python-1.2.3# python setup.py build实践出现:在包含自 _mysql.c:29 的文件中:pymemcompat.h:10:20: 错误:Python.h:没有那个文件或目录继续执行python setup.py install,可能出现以下错误: ======> pymemcompat.h:10:20: 致命错误:Python.h:没有那个文件或目录 编译中断。 出现这个问题的原因是,系统中的python是自带的,而没有安装python的开发包并没有安装,所以在shell下执行如下命令:点击(此处)折叠或打开yum install python-develInstalled size: 454 kIs this ok [y/N]: yInstalled: python-devel.i686 0:2.6.6-29.el6_2.2 Complete! 再次:python setup.py build出错:
编辑 site.cfg ,把 mysql_config 那一行取消注释,并改为:mysql_config = ”mysql_config的路径“(locate mysql_config。查找到mysql_config的路径)python setup.py buildpython setup.py install出错:
找不到setuptools;
对应操作:
1、python2.6 ,导入sys模块。
2、print sys.path
将sys.path下增加一个软链接。该链接指向settools中的setup.py
安装:python setup.py installFinished processing dependencies for MySQL-python==1.2.3安装结束后,打开 python2.6 输入import MySQLdb (注意:MySQLdb中的y是小写)如果没够报错就说明安装成功了。下面是一个批量导入数据到数据库的例子(myql.py):
1 #!/usr/bin/env python2.6 2 #encoding=utf-8 3 4 import MySQLdb 5 6 try: 7 conn = MySQLdb.connect(host='10.10.13.162', user = 'root', passwd='123456',port=3306, charset='utf8') 8 9 #使用cursor()方法获取操作游标10 cur = conn.cursor()11 conn.select_db('weixin')12 13 values = []14 file_object = open('./aa.txt', 'r')15 i = 2 16 for eachLine in file_object.readlines():17 tt = eachLine.strip()18 i = i+1 19 values.append((i, tt))20 cur.executemany('insert into test values(%s, %s)', values)21 22 conn.commit()23 cur.close()24 conn.close()25 except MySQLdb.Error, e:26 print 'error'
执行:python2.6 mysql.py
1 #!/usr/bin/env python 2 #coding=utf-8 3 ###################################### 4 # 5 # @author migle 6 # @date 2010-01-17 7 # 8 ###################################### 9 #10 # MySQLdb 查询11 #12 #######################################13 14 import MySQLdb15 16 conn = MySQLdb.connect(host='localhost', user='root', passwd='longforfreedom',db='python')17 18 cursor = conn.cursor()19 20 count = cursor.execute('select * from test')21 22 print '总共有 %s 条记录',count23 24 #获取一条记录,每条记录做为一个元组返回25 print "只获取一条记录:"26 result = cursor.fetchone();27 print result28 #print 'ID: %s info: %s' % (result[0],result[1])29 print 'ID: %s info: %s' % result 30 31 #获取5条记录,注意由于之前执行有了fetchone(),所以游标已经指到第二条记录了,也就是从第二条开始的所有记录32 print "只获取5条记录:"33 results = cursor.fetchmany(5)34 for r in results:35 print r36 37 print "获取所有结果:"38 #重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,39 cursor.scroll(0,mode='absolute')40 #获取所有结果41 results = cursor.fetchall()42 for r in results: 43 print r #结果是元组类型,注意44 cursor.close()45 conn.close()