windows下python3.8连接mysql

windows下python3.8连接mysql

​ 刚刚使用python连接数据库,首先安装**mysql-connector,**mysql-connector是 MySQL 官方提供的驱动器。

​ 我们可以使用 pip 命令来安装 mysql-connector,在window的dos命令窗口直接执行(前提是已经安装了pip,并且电脑环境变量有pip的执行位置):

python -m pip install mysql-connector

​ 使用以下代码测试 mysql-connector 是否安装成功:

import mysql.connector
然后安装pymysql

在window的dos命令窗口直接执行python pip.exe install pyMysq

出现successfully installed pyMysql 即安装成功

在cmd交互模式下输入import pymysql进行检测,若安装不成功会报错在pycharm中导入pymysql包
点击file–》setting–》project–》project interpreter–》
点击右上方的加号–》输入pyMysql–》点击install package

在pycharm中新建一个py文件输入import pymysql运行没有报错即导入成功

![img](file:///C:\Users\10305\AppData\Roaming\Tencent\Users\1030554333\QQ\WinTemp\RichOle\6DN%JX4S1NXI(YLEW$W4D.png)

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Windows 下使用 Python 3.8 连接 MySQL 数据库,可以使用 PythonMySQL 连接器,如 mysql-connector-python 或 PyMySQL。这里以 mysql-connector-python 为例,介绍连接 MySQL 数据库的步骤。 1. 安装 mysql-connector-python 可以使用 pip 命令安装 mysql-connector-python: ``` pip install mysql-connector-python ``` 2. 导入 MySQL 连接器 在 Python 脚本中导入 mysql.connector 模块: ``` import mysql.connector ``` 3. 连接 MySQL 数据库 创建一个 MySQL 连接对象,使用 mysql.connector.connect() 方法连接 MySQL 数据库。需要指定 MySQL 数据库的主机名、用户名、密码和数据库名: ``` mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) ``` 4. 执行 SQL 查询 使用 MySQL 连接对象的 cursor() 方法创建游标对象,使用游标对象的 execute() 方法执行 SQL 查询: ``` mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x) ``` 完整的代码示例: ``` import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x) ``` 注意:在连接 MySQL 数据库时,需要替换 host、user、password 和 database 的值为实际的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

curtain灬、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值