Python中pymysql模块通过获取mysql数据库命令行游标执行数据库命令来进行数据库操作

  优点:操作数据库语句所见即所得,执行了什么数据库语句都很清楚

  缺点:操作繁琐,代码量多

1. pymysql的基本使用

# -*- coding:utf-8 -*-
# Author:Wong Du import pymysql # 创建链接,相当于建立一个socket
conn = pymysql.Connection(host='10.0.0.100', port=3306, user='root', passwd='123456', db='testdb') # 建立游标,相当于进入 mysql> 命令操作界面
cursor = conn.cursor() # 建表,和mysql命令行操作一样
try:
create_table = cursor.execute('''create table student(
id int not null primary key auto_increment,
name char(32) not null,
register_date date not null DEFAULT "2018-05-09" );
''')
except pymysql.err.InternalError as e:
# print(type(e))
print("\033[31;1m%s; Do nothing...\033[0m" %e) # 插入数据
insert = cursor.execute('insert into student (name,register_date) values("junry", "2017-03-14");')
insert2 = cursor.execute('insert into student (name,register_date) values("hongfa", "2015-03-14");')
insert3 = cursor.execute('insert into student (name,register_date) values("jinglin", "2016-03-14");') # 查看表数据
select = cursor.execute('select * from student;')
for line in cursor.fetchall():
print(line) # 修改表数据
update = cursor.execute('update student set name="junwei" where id=1')
select2 = cursor.execute('select * from student;')
print(cursor.fetchone()) # 删除表数据
delete = cursor.execute('delete from student;')
select3 = cursor.execute('select * from student;')
if cursor.fetchall():
print(cursor.fetchall())
else:
print("This is a empty table...") # 提交
conn.commit() cursor.close() # 关闭游标
conn.close() # 关闭连接 # 等等 等等。。。

循环插入数据

# -*- coding:utf-8 -*-
# Author:Wong Du import pymysql # 建立连接
conn = pymysql.Connect(host='10.0.0.100', port=3306, user='root', passwd='123456', db='testdb') # 创建游标
cursor = conn.cursor() # 循环插入列表
many_list = [
('zhangsan', '2011-11-11'),
('lisi', '2012-11-11'),
('wangwu', '2022-10-09'),
] # 循环插入(插入多条内容)
cursor.executemany("insert into student (name, register_date) VALUE(%s, %s);", many_list) # 修改游标位置
cursor.scroll(1, mode='relative') # 相对移动,默认为relative
cursor.scroll(1, mode='absolute') # 绝对移动 # fetchone()获取一行数据、fetchmany(num)获取指定行数据、fetchall()获取所有行数据
cursor.execute("select * from student;")
for line in cursor.fetchall():
print(line) # 清楚student表的数据
cursor.execute("delete from student;") # 提交
conn.commit() cursor.close() # 关闭游标
conn.close() # 关闭连接

Python中pymysql基本使用的更多相关文章

  1. python中pymysql使用

    python中pymysql使用 https://blog.csdn.net/johline/article/details/69549131 import pymysql # 连接数据库 conne ...

  2. Python中pymysql模块详解

    安装 pip install pymysql 使用操作 执行SQL #!/usr/bin/env pytho # -*- coding:utf-8 -*- import pymysql # 创建连接 ...

  3. python中pymysql executemany 批量插入数据

    import pymysqlimport timedb = pymysql.connect("IP","username","password&quo ...

  4. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  5. (转)Python中操作mysql的pymysql模块详解

    原文:https://www.cnblogs.com/wt11/p/6141225.html https://shockerli.net/post/python3-pymysql/----Python ...

  6. python中pymsql常用方法(1)

    python中pymysql模块常用方法以及其使用 首先我们知道pymysql 是python中操作数据库的模块 使用步骤分为如下几步: ​ 1.与数据库服务器建立链接 conn=pymysql.Co ...

  7. python之pymysql模块学习(待完善...)

    pymysql介绍 pymysql是在python3.x版本中用于连接mysql服务器的一个库.python2中则使用mysqldb. 安装方法: pip install pymysql 使用实例: ...

  8. Python中的SQLAlchemy

    在Python中,使用SQLAlchemy可以对数据库进行操作. SQLAlchemy是Python中的一个标准库. 要使用SQLAlchemy,首先要创建连接: url = mysql+pymysq ...

  9. python的pymysql使用方法【转】

    前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.6.6.m ...

随机推荐

  1. (数据科学学习手札129)geopandas 0.10版本重要新特性一览

    本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 就在前不久,我们非常熟悉的Python地理 ...

  2. Spring Cloud Alibaba 的服务注册与发现

    Spring Cloud Alibaba 服务发现例子 一.需求 1.提供者完成的功能 2.消费者完成的功能 3.可以附加的额外配置 二.实现步骤 1.总的依赖引入 2.服务提供者和发现者,引入服务发 ...

  3. (二)、Docker 快速入门

    文档:https://docs.docker.com/install/linux/docker-ce/centos/ 中文文档:https://docs.docker-cn.com/engine/in ...

  4. 震惊,本Orzer下阶段直接怒送四个笑脸

    众所周知,在hzoi帝国中,Wzx是最菜的.那么究竟有多菜呢?下面就和小编一起来看看吧. 近日,hzoi最菜的wzx在第四阶段竟然怒送4个笑脸,同机房神犇直呼wzx太菜了! 以上就是wzx第四阶段怒送 ...

  5. Envoy实现.NET架构的网关(三)代理GRPC

    什么是GRPC gRPC是一种与语言无关的高性能远程过程调用 (RPC) 框架.gRPC 的主要好处是: 现代.高性能.轻量级的 RPC 框架. 契约优先的 API 开发,默认使用协议缓冲区,与语言无 ...

  6. 分析pcap包(基于UDP)

    //c代码#include <stdlib.h> #include <stdio.h> #include <pcap.h> #include <string. ...

  7. hdu 2086 A1 = ? (公式推导)

    有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n).若给出A0, An+1, 和 C1, C2, .....Cn.请编程计算A1 = ? Inp ...

  8. 恶意代码分析实战四:IDA Pro神器的使用

    目录 恶意代码分析实战四:IDA Pro神器的使用 实验: 题目1:利用IDA Pro分析dll的入口点并显示地址 空格切换文本视图: 带地址显示图形界面 题目2:IDA Pro导入表窗口 题目3:交 ...

  9. telnet IP 端口 的作用

    测试远程服务器的端口是否开启

  10. php swoft redis 发布和订阅

    //订阅 public function subscribe() { /* @var \Swoft\Redis\Redis $redis */ $redis = App::getBean(\Swoft ...