在Python中操作MySQL数据库,操作我们通常需要使用一些第三方库来帮助我们完(wan)成(cheng)这个任务,据库这些库可以帮助我(wo)们建立与MySQL数据库的依赖连接,执行SQL语句,包制获取查询结果等,作依在Python中,赖包最(zui)常用的操作操作(zuo)MySQL的库是pymysql和mysqlconnectorpython。(图片来源网络,据库侵删)
pymysql

pymysql是依赖一个纯Python实现(xian)的MySQL客户端库,它(ta)是(shi)包制由Python开发者创建并维护的(de)。pymysql支持Python 2.7和3.4+版本,作依并且支持所有MySQL 5.6+版本的赖包功能(neng)。

安装pymysql可(ke)以使用pip命令:

pip install pymysql
使用pymysql连接MySQL数据库的操作代码如(ru)下:
import pymysql创建连接conn = pymysql.connect(host='localhost', user='root', password='password', db='dbname')创建游标cursor = conn.cursor()执行(xing)SQL语句sql = "SELECT * FROM tablename"cursor.execute(sql)获取查询结果results = cursor.fetchall()for row in results: print(row)关闭游标和连接cursor.close()conn.close()
mysqlconnectorpython
mysqlconnectorpython是MySQL官方(fang)提(ti)供的Python驱动,它支持(chi)Python 2.7,据库 3.4, 3.5, 3.6和3.7版本,并且支(zhi)持(chi)所有MySQL 5.0+版本的依赖功能。
安装mysqlconnectorpython可以使(shi)用(yong)pip命令:
pip install mysqlconnectorpython
使用mysqlconnectorpython连接MySQL数据库的代(dai)码如下:
import mysql.connector创建连(lian)接conn = mysql.connector.connect(user='root', password='password', host='localhost', database='dbname')创建游标cursor = conn.cursor()执行SQL语句sql = "SELECT * FROM tablename"cursor.execute(sql)获取查询结果results = cursor.fetchall()for row in results: print(row)关闭游标和连接cursor.close()conn.close()
制作依赖包
如果你想将上述操作MySQL的代码封装(zhuang)成一个函数或者类,然后发布到PyPI上(shang)供其他人使(shi)用,你需要将这些代码打包成一个Python依赖包,制作依赖包的过程包括编写setup.py文件,然(ran)后使用setuptools工具(ju)进行打包。
我们需要创建一个setup.py文件,内容如下(xia):
from setuptools import setup, find_packagessetup(name='yourpackagename', version='0.1', packages=find_packages(), install_requires=[]), # add your package's dependencies here, e.g.: [], ['numpy'], ['pandas>=0.15']],这里(li)暂时为空,因为我们没有其他依赖包。我们可以使用setuptools的build_ext命令来编译我们的代(dai)码:
python setup.py build_ext inplace
我(wo)们可以使用(yong)twine命令将我们的依赖包发布(bu)到PyPI上:
twine upload dist/* u yourusername p yourpassword r pypi # replace yourusername and yourpassword with your PyPI account's username and password, replace pypi with the Python package index you want to upload to, e.g., pypi or testpypi. If you don't have a PyPI account, you can create one at https://pypi.org/account/register/. The r option is optional and specifies the Python package index you want to upload to. If you don't specify it, twine will use the default Python package index (https://pypi.org/). The u option is optional and specifies your PyPI account's username. If you don't specify it, twine will use the current user's credentials for authentication. The p option is optional and specifies your PyPI account's password. If you don't specify it, twine will prompt you for your PyPI account's password when it needs it. The asterisk (*) is a wildcard that matches all files in the current directory that end with the extension specified by the t option (in this case, all files that end with the extension ".tar.gz"). The t option is optional and specifies the file extension of the files you want to upload. If you don't specify it, twine will use the default file extension (in this case, ".tar.gz"). The verbose option is optional and makes twine display more information about what it's doing during the upload process. If you don't specify it, twine will display only error messages and warnings. The noninteractive option is optional and makes twine not ask any interactive questions during the upload process (e.g., whether to overwrite an existing package). If you don't specify it, twine will ask you these questions if necessary (e.g., if you try to upload a package with the same name as an existing package). The repositoryurl option is optional and specifies the URL of the Python package index where you want to upload your package (e.g., https://pypi.org/ or https://testpypi.org/). If you don't specify it, twine will use the default Python package index (https://pypi.org/). The configfile option is optional and specifies the path of a configuration file that contains additional options for twine (e.g., options for authentication). If you don't specify it, twine will use the default configuration file (~/.pypirc). The skipexisting option is optional and makes twine not upload a package if it already exists on the Python package index (e.g., if you try to upload a package with the same name as an existing package). If you don't specify it, twine will upload a package even if it already exists on the Python package index (unless you also specify the noninteractive option). The nocheckhostname option is optional and makes twine not check the validity of the server's certificate when connecting to the Python package index (e.g., when using HTTPS). If you don't specify it, twine will check the validity of the server's certificate when connecting to the Python package index (unless you also specify the trustedhost option). The trustedhost option is optional and specifies a commaseparated list of trusted hosts for which twine should not check the validity of their server's certificate when connecting to them (e.g., https://pypi.org/ or https://testpypi