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——线程是什么?
前言:虽然工作了三年,但是几乎没有使用到多线程之类的内容.这其实是工作与学习的矛盾.我们在公司上班,很多时候都只是在处理业务代码,很少接触底层技术. 可是你不可能一辈子都写业务代码,而且跳槽之后新单位 ...
随机推荐
- ES通过API调整设置
1.查询es的设置信息 2.查询单个索引的设置 3.设置复制集为0
- Python中的staticmethod和classmethod
谈谈Python中的staticmethod和classmethod 首先值得说明的是staticmethod和classmethod都是python中定义的装饰器,用的时候需要在前面加@ 即@sta ...
- 使用python实现二分法查找
最近开始学习mit的python课程,其中手工实现的一个关于二分法查找的练习代码个人感觉比较有参考价值,贴上来分享交流一下. 主要功能是在1-100中自己猜测一个数值,随后系统产生数值看是否符合猜测, ...
- 使用AXIS2作为Client訪问WebService
使用AXIS2,能够方便的构建WebService的server端,也能够非常方便的作为Cilent,来訪问别的WebService. 以下依据工作中的经历,整理了一下,作为Cilent訪问WebSe ...
- case_for_if 各种嵌套相结合
注明:本文是参考其他相关文章 整理,完全尊重原著作 #!/bin/bash usage() { cat << EOF EOF } main() { echo "猜分数赢大奖(0- ...
- Hadoop基础学习(一)分析、编写并执行WordCount词频统计程序
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jiq408694711/article/details/34181439 前面已经在我的Ubuntu ...
- java.lang.UnsupportedClassVersionError: com/dw/Function : Unsupported major.minor version 52.0
本地环境中有jdk7和jdk8,当把jdk8改为jdk7时,出现如下错误,是jdk版本的问题,在Properties-->JAVA Compiler-中的Compiler compliance ...
- 最近采集写的一个超简单实用的HTML解析类
1. [文件] HtmlDom.php <?php$oldSetting = libxml_use_internal_errors( true ); libxml_clear_errors(); ...
- Custom Database Integration Guide
Introduction This document provides instructions for integrating Openfire authentication, users, and ...
- linux命令学习笔记(25):linux文件属性详解
Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组. 最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loc ...