您的位置:首页 > 编程语言 > Python开发

window 7 python 3.7 安装pymysql及用法

2019-01-09 18:03 561 查看
版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。 https://blog.csdn.net/xunzaosiyecao/article/details/86154561

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();

个人微信公众号:

作者:jiankunking 出处:http://blog.csdn.net/jiankunking

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: