有时候需要操作数据库,为了能使用统一的接口访问,我们采用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的学习的更多相关文章

  1. Rest API 开发 学习笔记(转)

    Rest API 开发 学习笔记 概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着 ...

  2. ava如何实现系统监控、系统信息收集、sigar开源API的学习(转)

    ava如何实现系统监控.系统信息收集.sigar开源API的学习(转) 转自:http://liningjustsoso.iteye.com/blog/1254584 首先给大家介绍一个开源工具Sig ...

  3. JavaSE中线程与并行API框架学习笔记——线程为什么会不安全?

    前言:休整一个多月之后,终于开始投简历了.这段时间休息了一阵子,又病了几天,真正用来复习准备的时间其实并不多.说实话,心里不是非常有底气. 这可能是学生时代遗留的思维惯性--总想着做好万全准备才去做事 ...

  4. TensorLayer官方中文文档1.7.4:API – 强化学习

    API - 强化学习¶ 强化学习(增强学习)相关函数. discount_episode_rewards([rewards, gamma, mode]) Take 1D float array of ...

  5. abp学习(四)——根据入门教程(aspnetMVC Web API进一步学习)

    Introduction With AspNet MVC Web API EntityFramework and AngularJS 地址:https://aspnetboilerplate.com/ ...

  6. 前后端分离&接口API设计学习报告

    接口API设计学习报告 15331023 陈康怡 什么是API? API即Application Programming Interface.API是一种通道,负责一个程序与另一个程序的沟通.而对于w ...

  7. Android API Guides 学习笔记---Application Fundamentals(一)

    今天开始学习google官网上的API guides ,主要读了Application Fundamentals这一章节,此章节介绍了一个App的基本组成,共包括四大部分内容. 1.      App ...

  8. ArcGIS API Reference & Flex API samples学习进度备忘

    书签:跳过:另外跳过的内容有待跟进 __________________学习资源: 1.http://help.arcgis.com/en/webapi/flex/apiref/index.html ...

  9. JavaSE中线程与并行API框架学习笔记1——线程是什么?

    前言:虽然工作了三年,但是几乎没有使用到多线程之类的内容.这其实是工作与学习的矛盾.我们在公司上班,很多时候都只是在处理业务代码,很少接触底层技术. 可是你不可能一辈子都写业务代码,而且跳槽之后新单位 ...

随机推荐

  1. 基于live555实现的RTSPServer对底层进行性能优化的方法

    在博客<EasyIPCamera高性能摄像机RTSP服务器RTSPServer解决方案>我介绍了基于live555实现的一套RTSPServer功能组件,当时开发者经过几个月的调试,已经将 ...

  2. Regularization: how complicated the model is? Regularization, measures complexity of model 使预测准确、平稳 predictive stable

    http://www.kdd.org/kdd2016/papers/files/rfp0697-chenAemb.pdf https://homes.cs.washington.edu/~tqchen ...

  3. adjA=(detA)A-1

    A–>adjA 连续性 反函数

  4. Ubuntu PPPoE拨号上网指定网卡

    Just follow these steps: Check that the ethernet cable is properly connected Open Terminal Run sudo ...

  5. GEO(地理信息定位)

    核心知识点: 1.GEO是利用zset来存储地理位置信息,可以用来计算地理位置之间的距离,也可以做统计: 2.命令:geoadd geopos geodist geohash georadius/ge ...

  6. 微信小程序开发:学习笔记[7]——理解小程序的宿主环境

    微信小程序开发:学习笔记[7]——理解小程序的宿主环境 渲染层与逻辑层 小程序的运行环境分成渲染层和逻辑层. 程序构造器

  7. CentOs7 配置nfs 系统

    一.介绍 NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,功能是让客户端通过网络访问不同主机上磁盘里的数据,主要用在类Unix系统上实现文件共享 ...

  8. Flash+XML前后按钮超酷焦点图

    在线演示 本地下载

  9. mvc Bundling 学习记录(一)

    参考博客:http://www.cnblogs.com/xwgli/p/3296809.html 这里要详细记录的是对于现有MVC项目进行Bundling功能 1  如果没有System.Web.Op ...

  10. codeforces 之 Little Pigs and Wolves

    B-Little Pigs and Wolvestime limit per test2 secondsmemory limit per test256 megabytesinputstandard ...