数据库——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数 ...
随机推荐
- Uva 10806 来回最短路,不重复,MCMF
题目链接:https://uva.onlinejudge.org/external/108/10806.pdf 题意:无向图,从1到n来回的最短路,不走重复路. 分析:可以考虑为1到n的流量为2时的最 ...
- 高精度进位制转换,Poj(1220)
转自ACdream. #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXS ...
- 解决Gearman 报sqlite3错误
删除了系统原带的sqlite3 ,到官网上下一个源码,重新编译安装sqlite3. 如:把sqlite3安装到 /usr/local/sqlite3tar zxf sqlite3.xxxx.tar.g ...
- 缓冲区溢出实战教程系列(三):利用OllyDbg了解程序运行机制
想要进行缓冲区溢出的分析与利用,当然就要懂得程序运行的机制.今天我们就用动态分析神器ollydbg来了解一下在windows下程序是如何运行的. 戳这里看之前发布的文章: 缓冲区溢出实战教程系列(一) ...
- 一、初始Object-C
一.OC概述 特点: 1没有包得概念 2关键字以@开头 3.拓展名 .m 二.第一个OC类 1,分为2个文件..m和.h文件 2. .m文件用来实现类 .h用来定义声明类 .h文件中得写法 //@i ...
- css的position定位终极总结
relative相对定位是相对于自己的位置定位,absolute绝对定位是向上级一级一级搜索有position属性的div,如果没有找到就相对于body定位
- 服务器操作nginx相关操作命令
服务器操作nginx相关操作命令 登录服务器: ssh root@0.0.0.0 -p 22100 启动nginx: /usr/local/nginx/sbin/nginx 查看nginx是否启动 p ...
- Maven - 依赖范围<scope></scope>
6种:
- 使用inotify-tools与rsync构建实时备份系统
使用inotifywait监控文件变动 inotifywait是 inotify-tools 包中提供的一个工具,它使用 inotify API 来监控文件/目录中的变动情况. 在archlinux上 ...
- ethereum(以太坊)(八)--Address
pragma solidity ^0.4.0; contract Test{ address _owner; uint160 _c; constructor() public{ _owner = 0x ...