pymysql的安装及使用

目录

一、什么是PyMySQL?

二、如何安装PyMySQL?

1、Pip install pymysql 常规安装

2、zip安装包

3、Whl包

4、pycharm引入pymyaql模块

三、Pymysql使用基本流程

1、要求

2、操作流程

3、操作流程具体代码

扫描二维码关注公众号,回复: 5603006 查看本文章

Python具有内置的SQLite支持。在早期Python版本一般都使用MySQLdb模块,但这个MySQL的流行接口与Python 3不兼容。因此,在教程中将使用PyMySQL模块

一、什么是PyMySQL?

PyMySQL是从Python连接到MySQL数据库服务器的接口。 它实现了Python数据库API v2.0,并包含一个纯Python的MySQL客户

端库。 PyMySQL的目标是成为MySQLdb的替代品。 简单理解就是,Pymysql是python操作mysql数据库的三方模块。

PyMySQL参考文档:http://pymysql.readthedocs.io/

二、如何安装PyMySQL?

1、Pip install pymysql 常规安装

pip install pymysql -i 国内的源

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/ 

豆瓣:http://pypi.douban.com/simple/

导出pip清单

Pip freeze > package.txt

批量安装

Pip install -r package.txt

2、zip安装包

Pip install -r package.txt

Python setup.py install

C++版本错误

3、Whl包

   https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

pip install wheel

4、pycharm引入pymyaql模块

三、Pymysql使用基本流程

1、要求

1、要有mysql数据库

2、要有python

3、要有pymysql

2、操作流程

1、创建数据库链接

2、创建游标

3、执行命令

4、关闭链接

3、操作流程具体代码:

import pymysql

#创建数据库链接
db = pymysql.connect(
    host = "localhost", #主机ip
    user = "root", #数据库用户
    password = "1111", #用户对应的密码
    database = "school", #对应的数据库
    port = 3306, #数据库端口,默认3306
    charset = 'utf8' #数据库编码
)
#创建游标:游标用于传递python给mysql的命令和mysql返回的内容
cursor = db.cursor()
#执行部分
exe = cursor.execute("show databases") #执行命令,返回查询的条数
print(exe)
#result = cursor.fetchone()
#result = cursor.fetchmany(3)
result = cursor.fetchall() #查询结果

print(result)
#关闭部分
db.commit() #链接提交,用于对数据库的增删改
cursor.close() #关闭游标
db.close() #关闭链接

猜你喜欢

转载自blog.csdn.net/weixin_44239541/article/details/88562662