Python安装MySQLdb

Ubuntu
Python2

$ pip install MySQL-python -i https://pypi.douban.com/simple
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.douban.com/simple
Collecting MySQL-python
  Downloading https://pypi.doubanio.com/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (108 kB)
     |################################| 108 kB 2.9 MB/s
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ZsNmju/mysql-python/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ZsNmju/mysql-python/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-q5ranR
         cwd: /tmp/pip-install-ZsNmju/mysql-python/
    Complete output (10 lines):
    sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-ZsNmju/mysql-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-ZsNmju/mysql-python/setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "/tmp/pip-install-ZsNmju/mysql-python/setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
$
$ # 安装Ubuntu系统对于MySQL需要的依赖包libmysqlclient-dev
$ sudo apt-get install libmysqlclient-dev
$
$ pip install MySQL-python -i https://pypi.douban.com/simple
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.douban.com/simple
Collecting MySQL-python
  Downloading https://pypi.doubanio.com/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (108 kB)
     |################################| 108 kB 3.7 MB/s
Building wheels for collected packages: MySQL-python
  Building wheel for MySQL-python (setup.py) ... done
  Created wheel for MySQL-python: filename=MySQL_python-1.2.5-cp27-cp27mu-linux_x86_64.whl size=85319 sha256=b4272b21f75fbdac9d7494ffc35208567b2c6fa11aaa4458943bee377c5aedfb
  Stored in directory: /home/gfk/.cache/pip/wheels/86/18/6c/d641f201b29e8a151b7b3c438cc4fcdd8e0a91398abf56c5cd
Successfully built MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5
$
$ # 测试
$ python
Python 2.7.12 (default, Oct  5 2020, 13:56:01)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import MySQLdb
>>> mysql_con = {
    'host': '192.168.1.101',
    'port': 3099,
    'username': 'name1',
    'password': 'pwd1',
    'database': 'db1',
    'charset': 'utf8'
}
>>>db = MySQLdb.connect(host=mysql_con['host'], user=mysql_con['username'], passwd=mysql_con['password'],
                         db=mysql_con['database'], port=mysql_con['port'], charset=mysql_con['charset'])
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()
    # 使用execute方法执行SQL语句
    query_sql = 'SELECT name_cn, def_cn FROM rest_hpoitem WHERE hpo_id=%s;'
    cursor.execute(query_sql, [hpo_id])
    # 使用 fetchone() 方法获取一条数据
    query_data = cursor.fetchone()
    # 关闭数据库连接
    db.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值