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 "", 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()

你可能感兴趣的:(Python3,Python)