zoukankan      html  css  js  c++  java
  • windows 环境下安装python MySQLdb

    使用Python访问MySQL,需要一系列安装

    Linux下MySQLdb安装见  

    Python MySQLdb在Linux下的快速安装

    http://blog.csdn.NET/wklken/article/details/7271019

    -------------------------------------------------------------

    以下是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)

    [sql] view plain copy
     
     print?
    1. CREATE TABLE `user` (  
    2.   `Id` int(11) NOT NULL AUTO_INCREMENT,  
    3.   `name` varchar(255) DEFAULT NULL,  
    4.   `age` varchar(255) DEFAULT NULL,  
    5.   PRIMARY KEY (`Id`)  
    6. ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;  
    [python] view plain copy
     
     print?
    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()  

     

  • 相关阅读:
    7
    go http请求库HttpRequest
    Golang设计模式
    深挖 go 之 for-range 排坑指南
    go在并发情况下使用map
    Redis知识点总结
    go 条件与循环结构
    数据分析的数据源
    go 生产者消费者模型与发布订阅模型
    go 文件与目录操作
  • 原文地址:https://www.cnblogs.com/duking1991/p/6120887.html
Copyright © 2011-2022 走看看