zoukankan      html  css  js  c++  java
  • python安装MySQLdb:在windows下或linux下(以及eclipse中pydev使用msqldb的配置方法)

    写的非常好,可以解决问题:

    windows下:http://blog.csdn.net/wklken/article/details/7253245

    linux下:http://blog.csdn.net/wklken/article/details/7271019

    另外,这篇作为总体配置方案非常好:http://lbxhappy.iteye.com/blog/816074



    MySQL for python编译好的版本,下载列表,:http://www.codegood.com/downloads


    安装好在Ipython command line里查看:import MySQLdb

    如果报错 no module则说明安装不成功。

    不报错安装成功

    可以调用如下命令查看mysql安装信息:

    dir(MySQLdb)  查看模块成员:

    ['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'DATETIME', 'DBAPISet', 'D
    ataError', 'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'FIELD_TYPE', 'Int
    egrityError', 'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER',
     'NotSupportedError', 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING',
     'TIME', 'TIMESTAMP', 'Time', 'TimeFromTicks', 'Timestamp', 'TimestampFromTicks'
    , 'Warning', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__
    name__', '__package__', '__path__', '__revision__', '__version__', '_mysql', 'ap
    ilevel', 'connect', 'connection', 'constants', 'debug', 'escape', 'escape_dict',
     'escape_sequence', 'escape_string', 'get_client_info', 'paramstyle', 'release',
     'result', 'server_end', 'server_init', 'string_literal', 'test_DBAPISet_set_equ
    ality', 'test_DBAPISet_set_equality_membership', 'test_DBAPISet_set_inequality',
     'test_DBAPISet_set_inequality_membership', 'thread_safe', 'threadsafety', 'time
    s', 'version_info']

    help(MySQLdb) 查看帮助文档


    print MySQLdb.version_info

    print MySQLdb.__doc__

    print MySQLdb._mysql

    print MySQLdb.__name__


    Eclipse中mysqldb找不到:

    上面都是在IDLE和python command line里检查安装MySQLdb。但此时Eclipse中还是不能用,pydev 还是会报Unresolved import,找不到该模块。

    Eclipse中设置PyDev加载模块:进入windows->Preferences->PyDev->Interpreter - Python

    弹出对话框:


    Libraies选项下:点击界面右下角按钮apply,如果Libraries 下面中没有导入MySQLdb,就自己手动点击导入,在安装路径一般都在Python27Libsite-packageslibMySQLdb。

    导入成功后,切换Forced Builtins选项卡下面,手工添加 MySQLdb 字段 点击apply强制编译。

    如果还是不行,在Libraries 下面自己手动添加MySQLdb的目录,再次强制编译即可。

      在装wxPython2.9-win32-py27 时, import wx 使用了上述方法后仍报错,原因是在安装wx2.9时,目录为Python27Libsite-packages*wxPython2.9-msi*wx; 只要把wx越过他的上级目录,直接放在site-packages下就可以解决问题了。



    MySQLdb的使用:

    MySQLdb用户指南: http://mysql-python.sourceforge.net/MySQLdb.html 
    MySQLdb文档: 
    http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb-module.html

    测试代码:

            import MySQLdb

      con= MySQLdb.connect(host='ip',user='usr',passwd='****',db='****')

      cursor =con.cursor()

      sql ="select * from table_name"

      cursor.execute(sql)

      row=cursor.fetchone()

      print row

      cursor.close()

      con.close()




    以下引自上面文章,是为自己记录。请到连接地址阅读。


    windows环境下的:

    1.      安装数据库mysql

    下载地址:http://www.mysql.com/downloads/

    可以顺带装个图形工具,我用的是MySQL-Front


    2.      安装MySQLdb

    好了,到了这一步,你有两个选择

    A.     安装已编译好的版本(一分钟)

    B.     从官网下,自己编译安装(介个…..半小时到半天不等,取决于你的系统环境以及RP)

    若是系统32位的,有c++编译环境的,自认为RP不错的,可以选择自己编译安装,当然,遇到问题还是难免的,一步步搞还是能搞出来的

    若是系统64位的,啥都木有的,建议下编译版本的,甭折腾

     

    2.1安装已编译版本:

    http://www.codegood.com/downloads

    根据自己系统下载,双击安装,搞定

    然后import MySQLdb,查看是否成功

     

    我的,win7,64位,2.7版本

    MySQL-python-1.2.3.win-amd64-py2.7.exe

     

    2.2自己编译安装

    话说搞现成的和自己编译差距不一一点半点的,特别是64位win7,搞死了

     

    2.2.1安装setuptools

    在安装MySQLdb之前必须安装setuptools,要不然会出现编译错误

    http://pypi.python.org/pypi/setuptools

    http://peak.telecommunity.com/dist/ez_setup.py 使用这个安装(64位系统必须用这个)

     

    2.2.2安装MySQLdb

    下载MySQLdb

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

     

    解压后,cmd进入对应文件夹

    如果32位系统且有gcc编译环境,直接

    python setup.py build

     

    2.2.3问题汇总

    A. 64位系统,无法读取注册表的问题

    异常信息如下:

    F:devtoolsMySQL-python-1.2.3>pythonsetup.py build

    Traceback (most recent call last):

     File "setup.py", line 15, in <module>

       metadata, options = get_config()

     File "F:devtoolsMySQL-python-1.2.3setup_windows.py", line7, in get_config

       serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options[' registry_ke

    y'] )

    WindowsError: [Error 2] The system cannotfind the file specified

     

    解决方法:

    其实分析代码,发现只是寻找mysql的安装地址而已  修改setup_windows.py如下

    注解两行,加入一行,为第一步mysql的安装位置

     

       #serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,options['registry_key'] )

       #mysql_root, dummy = _winreg.QueryValueEx(serverKey,'Location')

       mysql_root = r"F:devtoolsMySQLMySQL Server 5.5"

     

    B.没有gcc编译环境

    unable to find vcvarsall.bat

    解决方法:安装编译环境(一个老外的帖子)

    1)  First ofall download MinGW. Youneed g++compiler and MingW make in setup.

    2)  If youinstalled MinGW for example to “C:MinGW” then add “C:MinGWin”to your PATH in Windows.(安装路径加入环境变量)

    3)  Now startyour Command Prompt and go the directory where you have your setup.py residing.

    4)  Last andmost important step:

    setup.py install build --compiler=mingw32

    或者在setup.cfg中加入:
    [build]
        compiler = mingw32

     

    C.gcc: /Zl: No suchfile or directory错误

    异常信息如下

    F:devtoolsMinGWingcc.exe -mno-cygwin-mdll -O -Wall -Dversion_info=(1,2,3,'

    final',0) -D__version__=1.2.3"-IF:devtoolsMySQLMySQL Server 5.5include" -IC

    :Python27include -IC:Python27PC -c_mysql.c -o build emp.win-amd64-2.7Rele

    ase\_mysql.o /Zl

    gcc: error: /Zl: No such file or directory

    error: command 'gcc' failed with exitstatus 1

     

    参数是vc特有的编译参数,如果使用mingw的话因为是gcc所以不支持。可以在setup_windows.py中去掉
    /Zl  

     

    解决方法:

    修改setup_windows.py  改为空的

    #extra_compile_args = [ '/Zl' ]

        extra_compile_args = [ '' ]

     目前就遇到这几个问题,望补充

     

    3.  增删改查代码示例及结果(just for test)

    1. CREATE TABLE `user` (  
    2.   `Id` int(11) NOT NULL AUTO_INCREMENT,  
    3.   `namevarchar(255) DEFAULT NULL,  
    4.   `age` varchar(255) DEFAULT NULL,  
    5.   PRIMARY KEY (`Id`)  
    6. ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;  

    1. #-*- coding:utf-8 -*-  
    2. #dbtest.py  
    3. #just used for a mysql test  
    4. ''''' 
    5. Created on 2012-2-12 
    6.  
    7. @author: ken 
    8. '''  
    9. #mysqldb      
    10. import time, MySQLdb, sys    
    11.          
    12. #connect   
    13. conn=MySQLdb.connect(host="localhost",user="root",passwd="test_pwd",db="school",charset="utf8")    
    14. cursor = conn.cursor()      
    15.          
    16. #add  
    17. sql = "insert into user(name,age) values(%s,%s)"     
    18. param = ("tom",str(20))      
    19. n = cursor.execute(sql,param)      
    20. print n      
    21.          
    22. #更新      
    23. sql = "update user set name=%s where Id=9001"     
    24. param = ("ken")      
    25. n = cursor.execute(sql,param)      
    26. print n      
    27.   
    28. #查询      
    29. n = cursor.execute("select * from user")      
    30. for row in cursor.fetchall():      
    31.     for r in row:      
    32.         print r,     
    33. print ""  
    34.   
    35.   
    36. #删除      
    37. sql = "delete from user where name=%s"     
    38. param =("ted")      
    39. n = cursor.execute(sql,param)      
    40. print n      
    41. cursor.close()      
    42.          
    43. #关闭      
    44. conn.close()  


  • 相关阅读:
    Chrome 中的彩蛋,一款小游戏,你知道吗?
    Json对象与Json字符串互转(4种转换方式)
    [PHP自动化-进阶]005.Snoopy采集框架介绍
    [PHP自动化-进阶]004.Snoopy VS CURL 模拟Discuz.net登陆
    [PHP自动化-进阶]003.CURL处理Https请求访问
    [PHP自动化-进阶]002.CURL模拟登录带有验证码的网站
    [PHP自动化-进阶]001.CURL模拟登录并采集数据
    [注]2015中国程序员生存报告,你苦你先看@^@
    [JavaWeb基础] 016.Struts2 国际化配置
    [工具推荐]_iOS音频批量转换
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205385.html
Copyright © 2011-2022 走看看