window 7 python 3.7 安装pymysql及用法

本文介绍了在Windows 7上安装Python 3.7和PyMySQL的步骤,包括从Python官网下载安装,通过CMD验证Python版本,使用pip安装PyMySQL。接着展示了一个示例,详细说明如何配置数据库连接参数,创建连接,并执行SQL语句进行数据插入操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、下载安装包,进行安装
https://www.python.org/downloads/windows/

2、打开cmd,输入python,查看python查看版本

在这里插入图片描述
3、安装PyMySQL
在cmd命令行中输入以下命令,进行安装

pip install PyMySQL

在这里插入图片描述
4、示例
#!/usr/bin/env python

-- coding: utf-8 --

import pymysql.cursors
import uuid
import datetime

连接配置信息

config = {
‘host’: ‘127.0.0.1’,
‘port’: 3306,
‘user’: ‘jiankunking’,
‘password’: ‘12345678’,
‘db’: ‘test’,
‘charset’: ‘utf8mb4’,
‘cursorclass’: pymysql.cursors.DictCursor,
}

创建连接

connection = pymysql.connect(**config)
counter = 1
n = 1000000

执行sql语句

try:
with connection.cursor() as cursor:
while counter <= n:
# 执行sql语句,插入记录
now = datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’)
sql = ‘INSERT INTO project (project_id, project_name, domain_id, update_man,cluster_id,update_time,create_time,description) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)’
cursor.execute(sql, (str(uuid.uuid4()), “test”, str(uuid.uuid4()), ‘jiankunking’, ‘QD’,
now, now,
‘这是一条测试数据’));
# 没有设置默认自动提交,需要主动提交,以保存所执行的语句
connection.commit()
# print(“第 :” + str(counter))
counter += 1
finally:
connection.close();

原文链接:https://blog.youkuaiyun.com/jiankunking/article/details/86154561

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值