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).文档对象 ...
随机推荐
- mysql无法启动ERROR! MySQL is running but PID file could not be found ?
转载于:http://blog.csdn.net/wuzhilon88/article/details/17616635 第一种方法:可能是硬盘满了,清理下垃圾文件. 第二种: 查看下数据库运行状态 ...
- Spring 中bean的作用、定义
Spring 中bean的作用.定义: 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的"配方(recipe)".把bean定义看成一个配方很有意义,它与cl ...
- 【MyBatis学习09】高级映射之一对多查询
上一篇博文总结了一下一对一的映射,本文主要总结一下一对多的映射,从上一篇文章中的映射关系图中可知,订单项和订单明细是一对多的关系,所以本文主要来查询订单表,然后关联订单明细表,这样就有一对多的问题出来 ...
- SGU180:Inversions(树状数组)
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount o ...
- 无线网络RSSI、SSID、BSSID
获取无线网络,及无线网络的参数之前,我们先了解一下RSSI,SSID和BSSID分别是什么,当然你可以去百度或者维基百科查阅,我这里只是简单的说明一下.RSSI就是无线网络的信号强度,这个是和无线AP ...
- mongo views
db.itemsView.drop(); db.items.aggregate([ { "$match": { "status": "true&quo ...
- select option 不可以选
<select> <option>Volvo</option> <option>Saab</option> <option disab ...
- C字符串复制
void mystrcpy(char *from, char *to) { for(; *from != '\0'; from++, to++) { *to = *from; } *to = '\0' ...
- Laragon集成开发环境+配置Xdebug+postman运行Xdebug
[ Laravel 5.5 文档 ] 快速入门 —— 使用 Laragon 在 Windows 中搭建 Laravel 开发环境:http://laravelacademy.org/post/7754 ...
- 构造方法与构造块的执行顺序(区别于static)
小面试题:在类的实例化时,会调用类的构造块(类中的构造块)和构造方法,无论构造方法在前还是在后,都先执行构造块 class Person{ public Person(){ System.out.pr ...