1.数据库连接

# 创建连接
def create_conn():
import pymysql
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='root',
database='py',
charset='utf8'
)
# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
return conn, cursor
# 关闭连接
def close_conn(conn, cursor):
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()

2.增

# 单条插入数据
def InsertOneDB(sql, data):
conn, cursor = create_conn()
cursor.executemany(sql, data)
conn.commit()
close_conn(conn, cursor) sqlInsertOneDB = "insert into user444 (name,age) values (%s,%s);"
data = [('john', 26)]
InsertOneDB(sqlInsertOneDB, data)
# 批量添加数据
def InsertManyDB(sql, data):
conn, cursor = create_conn()
cursor.executemany(sql, data)
conn.commit()
close_conn(conn, cursor) data = [
('apollo', ''),
('jack', ''),
('merry', '')
]
sql = 'insert into user444(name,age) values(%s,%s);'
InsertManyDB(sql, data)

3.删

# 删除数据
def DeleteOneDB(sql, data):
conn, cursor = create_conn()
print(sql)
cursor.execute(sql, data)
conn.commit()
close_conn(conn, cursor) # 定义将要执行的SQL语句
sql = "delete from user333 where name=%s;"
data = [("apollo11")]
DeleteOneDB(sql, data)

4.改

# 更改数据
def UpdataOneDB(sql, data):
conn, cursor = create_conn()
print(sql)
cursor.execute(sql, data)
conn.commit()
close_conn(conn, cursor) sql = "update user333 set name=%s where name=%s;"
data = ['Lily', 'jack11']
UpdataOneDB(sql, data)

5.查

# 查询数据
def SelectDB(sql, action, limit=None):
conn, cursor = create_conn()
cursor.execute(sql)
if action == 'fetchone':
ret = cursor.fetchone() # 取一条
elif action == 'fetchmany' and limit != None:
ret = cursor.fetchmany(limit) # 取三条
else:
ret = cursor.fetchall() # 取全部
for item in ret:
print(item)
close_conn(conn, cursor) # 执行查询的sql语句
sql = "select name,age from user333;"
# action可选值:fetchone,fetchmany,fetchall
# fetchmany必须提供limit参数
action = 'fetchmany'
# 取数据库前3条数据
SelectDB(sql, action, 3)
# fetchall取所有数据
SelectDB(sql, action)
# fetchone取一条数据
SelectDB(sql, action)

6.创建表SQL

create table userinfo(
id int auto_increment primary key ,
name varchar(32) not null unique ,
age int(2)
)engine =innodb default charset = utf8;
#注意:charset='utf8' 不能写成utf-8

pymysql连接数据库,实现数据库增删改查的更多相关文章

  1. 使用django连接数据库 对数据库 增删改查

    如果路由访问的时候出现 就把项目中的注释掉 登录功能 1 路由访问如果不加斜杠 会内部自动重定向加斜杠的路由 所有的静态文件(css,js,前端第三方类库)默认都放在static文件下 #静态文件配置 ...

  2. pyhton 自动化pymysql操作mysqldb数据库增删改查封装

    # coding=utf-8 import pymysql import os import configparser """ /* @:param: python ve ...

  3. go——beego的数据库增删改查

    一直都不理解使用go语言的时候,为什么还要自己去装beego,以为使用go便可以解决所有的问题,结果在朋友的点拨下,才意识到: go与beego的关系就好比是nodejs与thinkjs的关系,因此也 ...

  4. Java连接MySQL数据库增删改查通用方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. Java连接MySQL数据库增删改查通用方法 运行环境:eclipse+MySQL 以前我们Java连接MySQL数据库都是一个数据库写一个类,类 ...

  5. python操作mysql数据库增删改查的dbutils实例

    python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...

  6. mongodb 数据库 增删改查

    mongodb    数据库      增删改查 增: // 引入express 模块 var express = require('express'); // 路由var router = expr ...

  7. NX二次开发-NX访问SqlServer数据库(增删改查)C#版

    版本:NX9+VS2012+SqlServer2008r2 以前我写过一个NX访问MySQL数据库(增删改查)的文章https://www.cnblogs.com/nxopen2018/p/12297 ...

  8. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  9. 2. MongoDB基本操作 —— 用Mongo.exe操作数据库增删改查

    一.开篇 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象 ...

随机推荐

  1. Failed to load http://localhost:8080/team.php: Request header field x-jwt-header is not allowed by Access-Control-Allow-Headers in preflight response.

    axios 加入header之后,请求出现 Failed to load http://localhost:8080/team.php: Request header field x-jwt-head ...

  2. iOS swift版本无限滚动轮播图

    之前写过oc版本的无限滚动轮播图,现在来一个swift版本全部使用snapKit布局,数字还是pageConrrol样式可选 enum typeStyle: Int { case pageContro ...

  3. mySQL 开启事件存储过程

    怎样在Navicat中设置,是数据库按照记录中的日期更新状态字段 其实这个很常用,比如你网站里的某条记录的日期——比如说数据库中某条活动记录的审核日期字段已经过期,亦即当前时间已经超过审核日期,那么定 ...

  4. golang解析xml

    解析xml标签或者html标签,都是xml文档格式.要是返回的html标签,可以用第三方依赖库goquery来解析. 下面说下,解析xml标签的格式.直接上代码,代码如下: package main ...

  5. 优先队列——二项队列(binominal queue)

    [0]README 0.1) 本文文字描述部分转自 数据结构与算法分析, 旨在理解 优先队列——二项队列(binominal queue) 的基础知识: 0.2) 本文核心的剖析思路均为原创(inse ...

  6. 通信协议之广播---recvfrom 放回客户端的ip地址第一次全为0.0.0.0

    遇到一个很恶心的问题. while(1) { recvfrom(sockfd, readbuff, UDP_CMD_BUFF_LENGTH - 1, 0, (struct sockaddr *)&am ...

  7. CentOS 6.5 MySQL5.6.26源码安装

    一.源码安装cmake工具 从mysql5.5起,mysql源码安装开始使用cmake wget http://cmake.org/files/v3.2/cmake-3.2.3.tar.gztar z ...

  8. easyui datagrid onLoadSuccess加载两次。。

    今天使用EasyUI的datagrid时发现首次打开页面时onLoadSuccess方法执行了两次.后来发现主要问题是datagrid被初始化了两次.主要原因是一开始html中声明了dg为easyui ...

  9. idea Plugin "Maven Integration Extension" was not loaded: required plugin "Maven Integration" is disabled

    由于自己运行了eclipse maven及idea maven 同时操作,可能产生了以上错误.既: idea  Plugin "Maven Integration Extension&quo ...

  10. class 中构造函数与析构函数

    import pymysql class My_sql(): def __init__(self, host, user, passwd, db, port=3306, charset='utf8') ...