python数据库实例_Python3.6简单的操作Mysql数据库的三个实例

安装pymysql

参考:https://github.com/PyMySQL/PyMySQL/

pip install pymsql

实例一

import pymysql

# 创建连接

# 参数依次对应服务器地址,用户名,密码,数据库

conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='demo')

# 创建游标

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 执行语句返回影响的行数

effect_row = cursor.execute("select * from course")

print(effect_row)

# 获取所有数据

result = cursor.fetchall()

result = cursor.fetchone() # 获取下一个数据

result = cursor.fetchone() # 获取下一个数据(在上一个的基础之上)

# cursor.scroll(-1, mode='relative') # 相对位置移动

# cursor.scroll(0,mode='absolute') # 绝对位置移动

# 提交,不然无法保存新建或者修改的数据

conn.commit()

# 关闭游标

cursor.close()

# 关闭连接

conn.close()

实例二

import pymysql

# 建立连接

conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='demo')

# 创建游标

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 插入一条数据 %s是占位符 占位符之间用逗号隔开

effect_row = cursor.execute("insert into course(cou_name,time) values(%s,%s)", ("Engilsh", 100))

print(effect_row)

conn.commit()

cursor.close()

conn.close()

实例三

import pymysql.cursors

# Connect to the database

connection = pymysql.connect(host='localhost',

user='user',

password='passwd',

db='db',

charset='utf8mb4',

cursorclass=pymysql.cursors.DictCursor)

try:

with connection.cursor() as cursor:

# Create a new record

sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"

cursor.execute(sql, ('webmaster@python.org', 'very-secret'))

# connection is not autocommit by default. So you must commit to save

# your changes.

connection.commit()

with connection.cursor() as cursor:

# Read a single record

sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"

cursor.execute(sql, ('webmaster@python.org',))

result = cursor.fetchone()

print(result)

finally:

connection.close()

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对聚米学院的支持。如果你想了解更多相关内容请查看下面相关链接

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279123.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python之钉钉机器人推送天气预报

通过Python脚本结合钉钉机器人,定时向钉钉群推送天气预报 #!/usr/bin/python # -*- coding: utf-8 -*- # Author: aikergdedu.ml # My blog http://m51cto.51cto.blog.com import requests import re import urllib2 import json import sys import osheaders {Co…

google +按钮_如何禁用或改善Google的Google+集成

google 按钮If you’ve used Google lately, you’ve probably seen Google taking over Google’s search results. You don’t have to put up with it — you can disable the integration, show better social-networking pages or hide those pesky Google notifications.…

P2680 运输计划

传送门 十分显然完成工作的时间和航耗时最长的运输计划有关 所以题目意思就是要求最大值最小 所以可以想到二分 把所有大于mid时间的航线打上标记,显然删边只能在所有这些航线的公共路径上 要如何快速打标记是个问题 二分已经有一个log,所以只能承受O(n)…

java 集合读写同步_JAVA多线程学习十六 - 同步集合类的应用

1.引言在多线程的环境中,如果想要使用容器类,就需要注意所使用的容器类是否是线程安全的。在最早开始,人们一般都在使用同步容器(Vector,HashTable),其基本的原理,就是针对容器的每一个操作,都添加synchronized来进行同…

Linux下的parted工具的使用 GPT分区安装系统

安装系统是安装前时候ctrlatlF2 fdisk -l parted select /dev/sdb mklabel msdos # 将GPT磁盘格式化为MBR磁盘 对大硬盘进行分区 xfs 和 ntfs Linux下的parted工具的使用也很简单,具体操作如下: rootme:/mnt# parted /dev/sda Using /dev/sda Welcome to…

ubuntu自定义菜单_如何自定义Ubuntu的每日消息

ubuntu自定义菜单Ubuntu displays an informative message, known as the message of the day, when a user logs in at the terminal. The MOTD is fully customizable — you can add your own text and other dynamic data. 当用户在终端上登录时,Ubuntu将显示信…

java避免使用orderby_java – @OrderBy在JPA中无法正常工作

OrderBy如何运作?它在以下代码中不起作用:Employee.javapackage com.semanticbits.pojo;import java.util.List;import javax.persistence.CascadeType;import javax.persistence.Embedded;import javax.persistence.Entity;import javax.persistence.Ge…

BigDecimal四舍五入与保留位

1.引言 借用《Effactive Java》这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算。他们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的。然而,它们没有提供完全精确的结果&#…

火狐web开发清楚缓存_如何使用Firefox的Web开发工具

火狐web开发清楚缓存Firefox’s Web Developer menu contains tools for inspecting pages, executing arbitrary JavaScript code, and viewing HTTP requests and other messages. Firefox 10 added an all-new Inspector tool and updated Scratchpad. Firefox的Web Develop…

Leetcode400Nth Digit第N个数字

在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字。 注意: n 是正数且在32为整形范围内 ( n < 231)。 示例 1: 输入: 3 输出: 3 示例 2: 输入: 11 输出: 0 说明: 第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是0&#xff0c;它是…

Java基类共同属性设置_多选择基类的访问属性-Java初学笔记

多选择基类的访问属性你现在知道在定义类的访间属性时可用的选择项&#xff0c;你希望使用这些类定义子类。你知道在类继承上这些属性所具有的效果&#xff0c;但是你如何决定到底应该使用哪一个呢?这里没有死板和现成的规则&#xff0c;你选择的访问属性取决于在将来你想用类…

IT:如何在Windows Server 2008 R2上安装Hyper-V虚拟化

Windows Server 2008 R2 and later releases of the product ship with a virtualization platform called Hyper-V, which works quite well since it’s built into Windows. Today we’re going to show you how to install it. Windows Server 2008 R2和更高版本的产品附带…

FineReport单行与数据库交互的方法

1. 问题描述 我们在做一张报表填报的时候经常会遇到需要在一行进行添加动作&#xff0c;将该行数据直接与数据库交互&#xff0c;执行存储过程过程。我们可以通过每一行增加帆软“插入”按钮实现插入动作&#xff0c;并且在控件事件中增加和数据库的交互&#xff0c;但当事件…

java cas volatile_每日一个知识点:Volatile 和 CAS 的弊端之总线风暴

每日一个知识点系列的目的是针对某一个知识点进行概括性总结&#xff0c;可在一分钟内完成知识点的阅读理解&#xff0c;此处不涉及详细的原理性解读。一、什么是总线风暴总线风暴&#xff0c;听着真是一个帅气的词语&#xff0c;但如果发生在你的系统上那就不是很美丽了&#…

SqlServer之代码块相关

转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/sqlserver-codeblock/ 一、go语句 Go语句是SqlServer中用来表示当前代码块结束提交并确认结果的语句。 Go语句不能和其他Sql命令卸载同一行上&#xff01; 定义的局部变量作用域局限在定义它的代码…

010 使用list和tuple

list Python内置的一种数据类型是列表&#xff1a;list。list是一种有序的集合&#xff0c;可以随时添加和删除其中的元素。 比如&#xff0c;列出班里所有同学的名字&#xff0c;就可以用一个list表示&#xff1a; >>> classmates [Michael, Bob, Tracy] >>&g…

IT:如何使用Server 2008 R2上的远程桌面服务设置自己的终端服务器

In today’s IT learning article, we are going to take a look at installing Terminal Services, otherwise known as Remote Desktop Services, on a Server 2008 R2 machine. 在今天的IT学习文章中&#xff0c;我们将介绍在Server 2008 R2计算机上安装终端服务(也称为远程…

java 中的chartdata_获取Helm Charts中的文件夹列表

获得了位于templates文件夹之外的配置文件列表&#xff0c;我们将其输入到如下的helm图表中&#xff1a;├── configs│ ├── AllEnvironments│ │ ├── Infrastructure│ │ └── Services│ │ ├── ConfigFile1│ │ ├── ConfigFile2│ ├…

Win10 jdk的安装以及环境变量的配置,及需要注意的坑

此篇文章献给自己&#xff0c;希望下次长点记性 最近本人终于有时间开始学习appium&#xff0c;并且开始在电脑上配置环境&#xff0c;第一步就是在我那刚装的Win10 系统上安装jdk&#xff0c;过程并不顺利&#xff0c;由于之前都是用的win7&#xff0c;几乎都是一路的下一步&a…

java部分服务出现异常_Java web service 异常

1.org/apache/commons/discovery/tools/DiscoverSingletonException in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/discovery/tools/DiscoverSingleton缺少&#xff1a;commons-logging和commons-discovery2.ojava.lang.NoClassDefFoundErr…