数据库——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数 ...
随机推荐
- 解决Jenkins的错误“The Server rejected the connection: None of the protocols were accepted”
1. 配置节点,配置好节点后,在节点机上运行已下载文件,双击执行,提示"The Server rejected the connection: None of the protocols w ...
- mmap内存映射
http://blog.csdn.net/kongdefei5000/article/details/70183119 内存映射是个很有用,也很有意思的思想.我们都知道操作系统分为用户态和内核态,用户 ...
- 快算24点,POJ(3983)
题目链接:http://poj.org/problem?id=3983 中文题,就不解释题意了. 类似之前的一篇博客,这里也不上解释了,直接上代码吧. #include <iostream> ...
- python 3+djanjo 2.0.7简单学习(二)--创建数据库和模型
我们紧接上次,这里将建立数据库,创建第一个模型提示:这里我们不需要去一直启动,django会在我们ctrl+s的时候自动刷新并启动服务,很方便吧 1.数据库配置 现在,打开 vote_mysite/ ...
- Python 连接、操作数据库
使用python3+pymysql 一.安装python3 a) 从网上下载安装吧 二.安装pymysql https://pypi.python.org/pypi/PyMySQL h ...
- CUDA中多维数组以及多维纹理内存的使用
纹理存储器(texture memory)是一种只读存储器,由GPU用于纹理渲染的图形专用单元发展而来,因此也提供了一些特殊功能.纹理存储器中的数据位于显存,但可以通过纹理缓存加速读取.在纹理存储器中 ...
- currency 过滤器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- django中的构造字典(二级菜单,评论树,购物车)
1.构造父子结构: 1.1需求样式 客户列表 customer_list /customer/list/ -----> 添加客户 customer_add /customer/add/ ---- ...
- iOS面试题总结(持续更新)
过段时间打算跳槽,找了一些面试题来做,在这里做个总结方便review,希望能对要面试的童鞋有帮助. 以下为面试题: 运行以下代码会有什么结果 NSString *str1 = @"str1& ...
- react手动搭建解析
1.创建项目名<react-progect> ->项目文件下创建package.json文件 ->项目下执行命令:cnpm initcnpm i webpack webpack ...