当前位置:网站首页>MySQL的pymysql操作
MySQL的pymysql操作
2022-07-19 03:13:00 【火眼猊】
PyMysql是一个纯Python实现的MySQL客户端库,支持兼容Python3,用于代替MySQLdb
查询操作
提前写在mysql中准备相对应的表和数据
CREATE DATABASE IF NOT EXISTS mydb17_pymysql;
USE mydb17_pymysql;
CREATE TABLE IF NOT EXISTS student(
sid INT PRIMARY KEY auto_increment,
sname VARCHAR(20),
age INT
);
INSERT INTO student VALUES(NULL,'宋江',30),(NULL,'武松',28),(NULL,'林冲',26);
查询
import pymysql
# 获取mysql连接
conn = pymysql.connect(host='localhost', port=3306, user='root', password=, database='mydb17_pymysql')
# 获取游标
cursor = conn.cursor()
# 执行SQL语句 返回值就是SQL语句在执行过程中影响的行数
sql = "select * from student;"
row_count = cursor.execute(sql)
print("SQL语句执行影响的行数%d"%row_count)
# 取出结果集中一行,返回的结果是一行
# print(cursor.fetchone())
# 取出结果集中的所有数据,返回一行数据
for line in cursor.fetchall():
print(line)
# 关闭游标
cursor.close()
增删改操作
import pymysql
# 获取mysql连接
conn = pymysql.connect(host='localhost', port=3306, user='root', password=, database='mydb17_pymysql')
# 获取游标
cursor = conn.cursor()
# 执行SQL语句 返回值就是SQL语句在执行过程中影响的行数
sql = "select * from student;"
row_count = cursor.execute(sql)
print("SQL语句执行影响的行数%d"%row_count)
# 取出结果集中一行,返回的结果是一行
# print(cursor.fetchone())
# 取出结果集中的所有数据,返回一行数据
for line in cursor.fetchall():
print(line)
# 关闭游标
cursor.close()
边栏推荐
- PAT乙级-B1007 素数对猜想(20)
- 【微信小程序】课程表案例--0基础版
- 超炫彩的生日快乐祝福C语言代码
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 1, sign in question Gadi
- 19 和为K的子数组
- Leetcode53. 最大子數組和
- Numpy Learning
- Stock account opening is free of charge. Is it safe to open an account online
- Memory forensics WP of 2022 network security national game
- Metaverse: the possibilities and limitations of defi in the metauniverse
猜你喜欢
MySQL MHA high availability cluster deployment
2022-07-18: what is the output of the following go language code? A:Groutine; B:Main; C:Goroutine; D:GoroutineMain。 package main import ( “f
數據庫系統概論總結複習
超炫彩的生日快乐祝福C语言代码
How to carry out engineering implementation of DDD Domain Driven Design
01. Valid parentheses
Fundamentals of C language: structure (elementary level)
基于yarn1.x的monorepo实践分享
Regression analysis model
LeetCode 0749. 隔离病毒 - 大模拟
随机推荐
达摩院提出时序预测新模型 | 凌云速递
MySQL MHA high availability cluster deployment
Mapbox GL JS主动刷新地图的方法
How to carry out engineering implementation of DDD Domain Driven Design
SoC第一个工程hello_world
Experiment 1: film analysis
Redis service configuration
Microservice 2-nacos registry
GCDQueue封装
LeetCode 0749. 隔离病毒 - 大模拟
对TableView的优化
Regression analysis model
哪个证券公司开户股票手续费低,哪个证券开户安全
Electron渲染进程向主进程发送消息
我想知道开户佣金多少,手机上开户安全吗
【电商运营】在社交媒体上发帖没人看?时间是关键!
Pat b-b1007 prime pair conjecture (20)
[e-commerce operation] post on social media and no one reads it? Time is the key!
mysql(34) : 不登录mysql执行sql
Leetcode53. 最大子數組和