天天看点

window 7 python 3.7 安装pymysql及用法

1、下载安装包,进行安装

​​https://www.python.org/downloads/windows/​​

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

window 7 python 3.7 安装pymysql及用法

3、安装PyMySQL

在cmd命令行中输入以下命令,进行安装

pip install PyMySQL      
window 7 python 3.7 安装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();
      

个人微信公众号: