数据库——pymysql模块的使用(13)
1.基本用法——建立链接,获取游标,执行sql语句,关闭
- 建立远程链接账号和权限
mysql> grant all on *.* to 'root'@'%' identified by '';
Query OK, rows affected, warning (0.40 sec) mysql> flush privileges;
Query OK, rows affected (0.23 sec)#-*- coding:utf-8 -*-
import pymysql
user = input("用户名:").strip()
pwd = input("密码:").strip()
#建立链接
conn = pymysql.connect(
host = "192.168.110.1",
port = 3306,
user = "root",
password = "",
db = "db1",
charset = "utf8"
) #拿游标
cursor = conn.cursor() #执行sql
sql = 'select * from user_info where name = %s and pwd = %s'
print(sql)
rows = cursor.execute(sql,(user,pwd))
#关闭
cursor.close()
conn.close() if rows:
print("登录成功")
else:
print("登录失败")示例
2.增删改
#-*- coding:utf-8 -*-
import pymysql
#建立链接
conn = pymysql.connect(
host = "192.168.110.1",
port = 3306,
user = "root",
password = "",
db = "db1",
charset = "utf8"
) #拿游标
cursor = conn.cursor() #执行sql
###########增############
# sql = 'insert into user_info(name,pwd) values (%s,%s)'
# #插入一条记录
# rows = cursor.execute(sql,('xxx',123))
# conn.commit() # #插入多条记录
# rows = cursor.executemany(sql,[('xyy','ba'),('yxy','abc'),('yyy','dhdf')])
# conn.commit()
# print(rows) ###########删############
# sql = "delete from user_info where id = %s ;"
# rows = cursor.execute(sql,(3,))
# conn.commit()
# print(rows) ###########改############ sql = 'update user_info set pwd = %s where name = "egon4" '
rows = cursor.execute(sql,'aaa')
conn.commit()
print(rows)
#关闭
cursor.close()
conn.close()示例
3.查
#-*- coding:utf-8 -*-
import pymysql
#建立链接
conn = pymysql.connect(
host = "192.168.110.1",
port = 3306,
user = "root",
password = "",
db = "db1",
charset = "utf8"
) #拿游标
# cursor = conn.cursor()
cursor = conn.cursor(pymysql.cursors.DictCursor)#以字典形式显示
#执行sql
###########查############
sql = 'select * from user_info;'
rows = cursor.execute(sql)
# print(rows)
#一次取一个
# print(cursor.fetchone()) #打印一条记录
# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchone()) # print(cursor.fetchmany(2))#一次取多个
# print(cursor.fetchall())#取所有 cursor.scroll(3,mode='absolute') # 相对绝对位置移动
# cursor.scroll(3,mode='relative') # 相对当前位置移动
print(cursor.fetchone())
cursor.scroll(1,mode='relative') # 相对当前位置移动
print(cursor.fetchone())
#关闭
cursor.close()
conn.close()示例——查
4.获取插入的最后一条数据的自增ID
#-*- coding:utf-8 -*-
import pymysql
#建立链接
conn = pymysql.connect(
host = "192.168.110.1",
port = 3306,
user = "root",
password = "",
db = "db1",
charset = "utf8"
) #拿游标
cursor = conn.cursor() #执行sql
###########增############
sql = 'insert into user_info(name,pwd) values (%s,%s)'
rows = cursor.executemany(sql,[('xyyx','ba'),('yxyx','abc'),('yyyx','dhdf')])
print(cursor.lastrowid)#在插入语句后查询
conn.commit() #关闭
cursor.close()
conn.close()
数据库——pymysql模块的使用(13)的更多相关文章
- 第二百七十九节,MySQL数据库-pymysql模块操作数据库
MySQL数据库-pymysql模块操作数据库 pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connec ...
- MySQL数据库-pymysql模块操作数据库
pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connect() 参数: host=数据库ip port= ...
- 使用python连接mysql数据库——pymysql模块的使用
安装pymysql pip install pymysql 使用pymysql 使用数据查询语句 查询一条数据fetchone() from pymysql import * conn = conne ...
- 数据库入门-pymysql模块的使用
一.pymysql模块安装 由于本人的Python版本为python3.7,所以用pymysql来连接数据库(mysqldb不支持python3.x) 方法一: #在cmd输入 pip3 instal ...
- 05 数据库入门学习-正则表达式、用户管理、pymysql模块
一.正则表达式 正则表达式用于模糊查询,模糊查询已经讲过了 like 仅支持 % 和 _ 远没有正则表达式灵活当然绝大多数情况下 like足够使用 #语法 select *from table whe ...
- pymysql模块使用---Python连接MySQL数据库
pymysql模块使用---Python连接MySQL数据库 浏览目录 pymysql介绍 连接数据库 execute( ) 之 sql 注入 增删改查操作 进阶用法 一.pymysql介绍 1.介绍 ...
- Python连接MySQL数据库之pymysql模块使用
安装PyMySQL pip install pymysql PyMySQL介绍 PyMySQL是在python3.x版本中用于连接MySQL服务器的一个库,2中则使用mysqldb. Django中也 ...
- Python连接MySQL数据库之pymysql模块
pymysql 在python3.x 中用于连接MySQL服务器的一个库:Python2中则使用mysqldb pymysql的模块的基本的使用 # 导入pymysql模块 import pymysq ...
- 第八章| 3. MyAQL数据库|Navicat工具与pymysql模块 | 内置功能 | 索引原理
1.Navicat工具与pymysql模块 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图形界面的形式操作MySQL数 ...
随机推荐
- POJ-3111 K Best---二分求最大化平均值
题目链接: https://cn.vjudge.net/problem/POJ-3111 题目大意: 卖宝救夫:Demy要卖珠宝,n件分别价值vi 重 wi,她希望保留k件使得 最大. 解题思路: # ...
- 使用node.js + socket.io + redis实现基本的聊天室场景
在这篇文章Redis数据库及其基本操作中介绍了Redis及redis-cli的基本操作. 其中的publish-subscribe机制应用比较广泛, 那么接下来使用nodejs来实现该机制. 本文是对 ...
- list 用法的随手记
在list 用法中.1. add是直接添加 一个变量.不能添加一个 集合元素,比如数组 这种写法是错误的 ,因为不能添加集合 这种写法是对的,因为直接添加元素 2. 但是addrannge 是添加一个 ...
- express_webpack自动刷新
现在,webpack可以说是最流行的模块加载器(module bundler).一方面,它为前端静态资源的组织和管理提供了相对较完善的解决方案,另一方面,它也很大程度上改变了前端开发的工作流程.在应用 ...
- nuget打包
[1.创建.nuspec文件] 在.csproj目录下运行命令 nuget spec [2.生成包nupkg] 在.csproj目录下运行命令 nuget pack xxxx.csproj [推送命令 ...
- [NVIDIA编程教程]OpenACC: Directives for GPUs
NVIDIA已经在过去五年里大力发展CUDA技术,我们估计CUDA开发人员超过15万,很多重要的科学应用正在CUDA的帮助下完成.但是我们仍然有一个很长的路要走,以帮助每个人从GPU计算中享受到好处. ...
- image retrieval数据集
1. Oxford,vgg组,主要是building方面的数据.http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/index.html 2. Calte ...
- 用servlet设计OA管理系统时遇到问题
如果不加单引号会使得除变量和int类型的值不能传递 转发和重定向的区别 转发需要填写完整路径,重定向只需要写相对路径.原因是重定向是一次请求之内已经定位到了服务器端,转发则需要两次请求每次都需要完整的 ...
- FreeBSD--网络配置
配置网口:ifconfig |less dc0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500 i ...
- request中的那些方法到底是干什么的?
最近做Java Web项目,在.jsp页面和servlet之间request和response还是有些混淆,查阅了一些资料,总结如下,方便以后使用: 首先,servlet接口是最基本的,提供的五个方法 ...