pythonDB api的学习
有时候需要操作数据库,为了能使用统一的接口访问,我们采用Python DB API,地址为
https://www.python.org/dev/peps/pep-0249/
全文参考---“疯狂的蚂蚁crazyant”
我使用的是mysql+pymysql+pycharm连接数据库,windows本地要安装mysql数据库
import pymysql conn = pymysql.connect(
host = '127.0.0.1',
port = 3307,
user = 'root',
password = 'root',
db = 'songqin',
charset = 'utf8', )
#获取一个游标
cursor = conn.cursor()
print(conn) #<pymysql.connections.Connection object at 0x000001F42FD05898>
print(cursor) #<pymysql.cursors.Cursor object at 0x000001F4319B59E8> cursor.close()
conn.close()
执行查询语句
import pymysql conn = pymysql.connect(
host = '127.0.0.1',
port = 3307,
user = 'root',
password = 'root',
db = 'songqin',
charset = 'utf8', )
#获取一个游标
cursor = conn.cursor()
sql = 'select * from user'
cursor.execute(sql) print(cursor.rowcount) rs = cursor.fetchone()
print(rs) #(1, 'name1') rs = cursor.fetchmany(3)
print(rs) #((2, 'name2'), (3, 'name3'), (4, 'name4')) rs = cursor.fetchall()
print(rs) #((5, 'name5'), (6, 'name6'), (7, 'name7'), (8, 'name8'), (9, 'name9')) cursor.close()
conn.close()
执行查询
import pymysql conn = pymysql.connect(
host = '127.0.0.1',
port = 3307,
user = 'root',
password = 'root',
db = 'songqin',
charset = 'utf8',
#cursorclass = pymysql.cursors.DictCursor
)
#获取一个游标
cursor = conn.cursor()
sql = 'select * from user'
cursor.execute(sql) rs = cursor.fetchall()
print(rs)
for i in rs:
#print('userid=%s,username=%s' % (i[0],i[1]))
print('userid=%s,username=%s' % i) cursor.close()
conn.close()
执行增删改
import pymysql conn = pymysql.connect(
host = '127.0.0.1',
port = 3307,
user = 'root',
password = 'root',
db = 'songqin',
charset = 'utf8',
#cursorclass = pymysql.cursors.DictCursor
)
#获取一个游标
cursor = conn.cursor()
sql_insert = "insert into user(userid,username) values(10,'name10')"
sql_update = "update user set username='name91' where userid=9"
sql_delete = "delete from user where userid<3" cursor.execute(sql_insert)
print(cursor.rowcount) cursor.execute(sql_update)
print(cursor.rowcount) cursor.execute(sql_delete)
print(cursor.rowcount)#受影响的行数
#发现数据库并没有改变,而让其改变的话,只能提交commit conn.commit() cursor.close()
conn.close()
异常回滚:
import pymysql conn = pymysql.connect(
host = '127.0.0.1',
port = 3307,
user = 'root',
password = 'root',
db = 'songqin',
charset = 'utf8',
#cursorclass = pymysql.cursors.DictCursor
)
#获取一个游标
cursor = conn.cursor()
sql_insert = "insert into user(userid,username) values(10,'name10')"
sql_update = "update user set username='name91' where userid=9"
sql_delete = "delete from user where userd<3" #这里故意写错 try:
cursor.execute(sql_insert)
print(cursor.rowcount) cursor.execute(sql_update)
print(cursor.rowcount) cursor.execute(sql_delete)
print(cursor.rowcount)#受影响的行数
#发现数据库并没有改变,而让其改变的话,只能提交commit conn.commit()
except Exception as e:
print(e)
conn.rollback() #数据回滚到之前的状态 cursor.close()
conn.close()
pythonDB api的学习的更多相关文章
- Rest API 开发 学习笔记(转)
Rest API 开发 学习笔记 概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着 ...
- ava如何实现系统监控、系统信息收集、sigar开源API的学习(转)
ava如何实现系统监控.系统信息收集.sigar开源API的学习(转) 转自:http://liningjustsoso.iteye.com/blog/1254584 首先给大家介绍一个开源工具Sig ...
- JavaSE中线程与并行API框架学习笔记——线程为什么会不安全?
前言:休整一个多月之后,终于开始投简历了.这段时间休息了一阵子,又病了几天,真正用来复习准备的时间其实并不多.说实话,心里不是非常有底气. 这可能是学生时代遗留的思维惯性--总想着做好万全准备才去做事 ...
- TensorLayer官方中文文档1.7.4:API – 强化学习
API - 强化学习¶ 强化学习(增强学习)相关函数. discount_episode_rewards([rewards, gamma, mode]) Take 1D float array of ...
- abp学习(四)——根据入门教程(aspnetMVC Web API进一步学习)
Introduction With AspNet MVC Web API EntityFramework and AngularJS 地址:https://aspnetboilerplate.com/ ...
- 前后端分离&接口API设计学习报告
接口API设计学习报告 15331023 陈康怡 什么是API? API即Application Programming Interface.API是一种通道,负责一个程序与另一个程序的沟通.而对于w ...
- Android API Guides 学习笔记---Application Fundamentals(一)
今天开始学习google官网上的API guides ,主要读了Application Fundamentals这一章节,此章节介绍了一个App的基本组成,共包括四大部分内容. 1. App ...
- ArcGIS API Reference & Flex API samples学习进度备忘
书签:跳过:另外跳过的内容有待跟进 __________________学习资源: 1.http://help.arcgis.com/en/webapi/flex/apiref/index.html ...
- JavaSE中线程与并行API框架学习笔记1——线程是什么?
前言:虽然工作了三年,但是几乎没有使用到多线程之类的内容.这其实是工作与学习的矛盾.我们在公司上班,很多时候都只是在处理业务代码,很少接触底层技术. 可是你不可能一辈子都写业务代码,而且跳槽之后新单位 ...
随机推荐
- 不依赖外部js es 库 实现 点击内容 切换
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta http-equiv=&qu ...
- 高德地图API开发二三事(一)如何判断点是否在折线上及引申思考
最近使用高德地图 JavaScript API 开发地图应用,提炼了不少心得,故写点博文,做个系列总结一下,希望能帮助到LBS开发同胞们. 项目客户端使用高德地图 JavaScript API,主要业 ...
- 全能,OnSize的使用,部分覆盖后重画,都没有问题
import wx class View(wx.Panel): def __init__(self, parent): super(View, self).__init__(parent) self. ...
- fusioncharts 用法实例
支持xml格式和json格式的数据. 用法很简单. 1.需要引入FusionCharts.js. 2.html中定义个id="chart"的div <div id=" ...
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
- 协程与IO多路复用
IO多路复用 I/O多路复用 : 通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作. Python Python中有一个select模块, ...
- ansible2
一.ansible模块(yum.pip.service.conr.user.group) 你是否知道ansible一共有多少模块呢?可以用以下命令查看: [root@localhost ~]# ans ...
- Redmine后台修改admin密码
Redmine后台修改admin密码 进入redmine安装目录,假设redmine安装在/var/www/html/redmine/目录下. cd /var/www/html/redmine/scr ...
- Linux环境下使用dosemu写汇编
本章学习内容是汇编语言,现在直接写汇编的机会不多了,但一定要能读懂,信息安全的核心思维方式“逆向”在这有很好很直接的体现,反汇编就是直接的逆向工程. 所以我在前几天的学习中在Ubuntu环境下安装了可 ...
- ubuntu mysql 配置(远程访问&&字符集设置&&忽略大小写)
1.安装 参考http://www.cnblogs.com/wuhou/archive/2008/09/28/1301071.html sudo apt-get install mysql-serve ...