pymysql连接数据库,实现数据库增删改查
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连接数据库,实现数据库增删改查的更多相关文章
- 使用django连接数据库 对数据库 增删改查
如果路由访问的时候出现 就把项目中的注释掉 登录功能 1 路由访问如果不加斜杠 会内部自动重定向加斜杠的路由 所有的静态文件(css,js,前端第三方类库)默认都放在static文件下 #静态文件配置 ...
- pyhton 自动化pymysql操作mysqldb数据库增删改查封装
# coding=utf-8 import pymysql import os import configparser """ /* @:param: python ve ...
- go——beego的数据库增删改查
一直都不理解使用go语言的时候,为什么还要自己去装beego,以为使用go便可以解决所有的问题,结果在朋友的点拨下,才意识到: go与beego的关系就好比是nodejs与thinkjs的关系,因此也 ...
- Java连接MySQL数据库增删改查通用方法
版权声明:本文为博主原创文章,未经博主允许不得转载. Java连接MySQL数据库增删改查通用方法 运行环境:eclipse+MySQL 以前我们Java连接MySQL数据库都是一个数据库写一个类,类 ...
- python操作mysql数据库增删改查的dbutils实例
python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...
- mongodb 数据库 增删改查
mongodb 数据库 增删改查 增: // 引入express 模块 var express = require('express'); // 路由var router = expr ...
- NX二次开发-NX访问SqlServer数据库(增删改查)C#版
版本:NX9+VS2012+SqlServer2008r2 以前我写过一个NX访问MySQL数据库(增删改查)的文章https://www.cnblogs.com/nxopen2018/p/12297 ...
- Yii2.0高级框架数据库增删改查的一些操作(转)
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- 2. MongoDB基本操作 —— 用Mongo.exe操作数据库增删改查
一.开篇 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象 ...
随机推荐
- spring实现定时任务
今天在项目里需要实现一个定时任务,每隔3个小时将过滤的广告通过邮件上报给运营一次.考虑了一下,从实现的技术上可以有三种做法: 1.Java自带的java.util.Timer类,这个类允许调度一个ja ...
- python 在Windows中描述路径时出现的问题
问题的根本:windows读取文件可以用\,但在字符串里面\被作为转义字符使用, python在描述路径时有两种方式: 'd:\\a.txt',转义的方式 r'd:\a.txt',声明字符串不需要 ...
- atitit.人脸识别的应用场景and使用最佳实践 java .net php
atitit.人脸识别的应用场景and使用最佳实践 java .net php 1. 人脸识别的应用场景1 2. 标准化的api1 3. 框架选型 JNI2OpenCV.dll and JavaCV ...
- Windows Mobile X图标如何销毁窗体而非隐藏
在Windows Mobile窗体上,有“OK”和“X”两种形式按钮.1.在Form的属性里,设置“MinimizeBox=false”,则窗体显示”OK”,点击该按钮窗体销毁并退出.2.设置“Min ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- jxta 2.8x启动了
http://chaupal.github.io/ ———————————————————————————————————————————————————————————————————— 至少两个月 ...
- Android实现短信监听并且转发到指定的手机号,转发后不留痕
转载:http://blog.csdn.net/swqqcs/article/details/7252419 通过这些代码也可以对远程手机实现短信控制.有兴趣的可以自己改一下,说一下简单的原理,要实现 ...
- jQuery插件-json2.js
from:http://blog.csdn.net/gjb724332682/article/details/46682743 前言 json2.js是一个json插件,下载地址:https://gi ...
- 深入了解UIAutomation 的API
有关UiAUiAutomation的API对象的文件名称. 1.UIAutomation中的对象都是以UIA#####开头的出现的.eg:UIAButton 2.有关Logger对象负责日志的输出 U ...
- iOS学习笔记(九)——xml数据解析
在iPhone开发中,XML的解析有很多选择,iOS SDK提供了NSXMLParser和libxml2两个类库,另外还有很多第三方类库可选,例如TBXML.TouchXML.KissXML.Tiny ...