数据库——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数 ...
随机推荐
- 【转】jpg png区别和使用
为什么想整理这方面的类容,我觉得就像油画家要了解他的颜料和画布.雕塑家要了解他的石材一样,作为网页设计师也应该对图片格式的特性有一定了解,这样才能更好的表达你的创意和想法. 除此之外,我们在平时工作中 ...
- Mybatis-generator自动生成
第一步:导入架包 <build> <plugins> <plugin> <groupId>org.mybatis.generator</group ...
- 基于建模的视觉定位(SFM-Based Positioning)
具体方法来自我参与的这篇journal: Vision-Based Positioning for Internet-of-Vehicles, IEEE Transactions on Intelli ...
- cudpp库的编译和使用
项目主页 http://cudpp.github.io/ 根据这个网址的提示进行 https://github.com/cudpp/cudpp/wiki/BuildingCUDPPwithCMake ...
- BZOJ4008: [HNOI2015]亚瑟王(期望dp)
Time Limit: 20 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 1952 Solved: 1159[Submit][Status] ...
- jstree 全部选中事件 select_all 使用
select_all function of jstree not checked node for jstree-open branch of ajax-jstree 很尴尬啊,找了整个百度,360 ...
- 基于mybatis设计简单信息管理系统2
1.空指针异常 public class CanvasServlet extends HttpServlet { private CanvasService canvasService; privat ...
- Q&A - 如何获取ip地址所在地
获取其IP地址后,传入以下URL,并请求该URL,该请求会响应一个JSON格式的数据包,该IP地址的所在地均在这个数据包内 http://int.dpool.sina.com.cn/iplooku ...
- 网络基础-交换机、路由器、OSI7层模型
第1章 网络基础 1.1 网络的诞生 网络的诞生使命:通过各种互联网服务提升全球人类生活品质. 让人类的生活更便捷和丰富,从而促进全球人类社会的进步.并且丰富人类的精神世界和物质世界,让人类最便捷地获 ...
- linux常见内核参数
参数 描述 net.ipv4.ip_forward 接口间转发报文net.ipv4.tcp_tw_reuse 表示是否允许将处于 TIME-WAIT 状态的 socket (TIME-WAIT 的端口 ...