mysql数据库(三)——pymysql模块

pymysql模块:本质就是一个套接字客户端软件,使用前需要事先安装

pip install pymysql

1.基本使用

import pymysql
user_name = input(‘name:‘).strip()
user_psw = input(‘psw:‘).strip()

# 1.建链接
conn = pymysql.connect(
    host=‘0.0.0.0‘,
    port=3306,
    user=‘root‘,
    password=‘xxx‘,
    database=‘db1‘,
    charset=‘utf8‘
    )
# 2.游标
cursor = conn.cursor() # 执行完毕,返回的结果集默认以元祖显示

# 3.执行sql语句
sql = "select * from usr where name=‘%s‘ and psw=‘%s‘"%(user_name,user_psw)
print(sql)
res = cursor.execute(sql)

cursor.close()
conn.close()

2.execute()之SQL注入

注意:符号--会注释掉它之后的sql,正确的语法:--后至少有一个任意字符

根本原理:就根据程序的字符串拼接name=‘%s‘,我们输入一个xxx‘ -- haha,用我们输入的xxx加‘在程序中拼接成一个判断条件name=‘xxx‘ -- haha

import pymysql
user_name = input(‘name:‘).strip()
user_psw = input(‘psw:‘).strip()

# 1.建链接
conn = pymysql.connect(
    host=‘0.0.0.0‘,
    port=3306,
    user=‘root‘,
    password=‘xxx‘,
    database=‘db1‘,
    charset=‘utf8‘
    )
# 2.游标
cursor = conn.cursor() # 执行完毕,返回的结果集默认以元祖显示

# 3.执行sql语句
sql = "select * from usr where name=%s and psw=%s"

res = cursor.execute(sql,(user_name,user_psw))
# execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了

cursor.close()
conn.close()

if res:
    print(‘登陆成功!‘)
else:
    print(‘登录失败!‘)

3.增加数据

import pymysql
conn = pymysql.connect(
    host=‘0.0.0.0‘,
    port=3306,
    user=‘root‘,
    password=‘xxx‘,
    database=‘db1‘
)

cursor = conn.cursor()
# sql = "insert into usr(name,psw) values(‘kong‘,‘hui123‘);"
# res = cursor.execute(sql)

sql = "insert into usr(name,psw) values(%s,%s);"
res = cursor.execute(sql,(‘张三‘,‘hui123‘))
# 增加单个数据

# res = cursor.executemany(sql,[(‘李四‘,‘hui123‘),(‘王五‘,‘hui123‘)])
# 添加多条记录

# print(res) # 返回影响行数

conn.commit() # 必须有提交操作

cursor.close()
conn.close()

4.查询数据

import pymysql
conn = pymysql.connect(
    host=‘0.0.0.0‘,
    port=3306,
    user=‘root‘,
    password=‘xxx‘,
    database=‘db1‘
)
cursor = conn.cursor() # 返回结果以元祖显示
# cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 返回结果以字典显示

sql = ‘select * from usr;‘
rows = cursor.execute(sql)

# 一个个的取查询结果
# res1 = cursor.fetchone()
# res2 = cursor.fetchone()
# res3 = cursor.fetchone()
# print(res1, res2, res3, sep=‘\n‘)

# 一次取多个结果
# res = cursor.fetchmany(3)
# for i in res:
#     print(i)

# 取全部
res = cursor.fetchall()
for i in res:
    print(i)

# cursor.scroll(2,mode=‘absolute‘) # 相对初始位置移动
# cursor.scroll(2,mode=‘relative‘) # 相对当前位置移动

cursor.close()
conn.close()

原文地址:https://www.cnblogs.com/zhao1126/p/10238287.html

时间: 2024-07-30 11:25:03

mysql数据库(三)——pymysql模块的相关文章

Python连接MySQL数据库之pymysql模块使用

Python连接MySQL数据库之pymysql模块使用 Python3连接MySQL PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. Django中也可以使用PyMySQL连接MySQL数据库. PyMySQL安装 pip install pymysql 连接数据库 注意事项 在进行本文以下内容之前需要注意: 你有一个MySQL数据库,并且已经启动. 你有可以连接该数据库的用户名和密码 你有一个有权限操作的datab

Python连接MySQL数据库之pymysql模块使用(1)

01Python登录.py username = input("输入用户名:")pwd = input("请输入密码:") # if username == "erge" and pwd == "dashabi":# print("登陆成功!")# else:# print("滚~") with open("userinfo.txt", "r",

mysql数据库结合pam_mysql模块实现vsftpd虚拟用户

mysql数据库结合pam_mysql模块实现vsftpd虚拟用户登录 最近开始学mysql,自己做一个小实验,来个总结,比较容易理解,没什么太多理论性的东西. 一.实验环境的准备 1. 先下载需要用到的软件和依赖包 [[email protected] ~]# yum -y install mariadb-server mariadb-devel pam-devel gcc-c++ vsftpd 2. 安装pam_mysql模块(Plugable Authentication Module 插

Python 3.5 连接Mysql数据库(pymysql 方式)

由于 MySQLdb 模块还不支持 Python3.x,官方的Mysql连接包只支持到3.4,所以 Python3.5 如果想连接MySQL需要安装 pymysql 模块. pymysql 模块可以通过 pip 安装. 由于Python统一了数据库连接的接口,所以 pymysql 和 MySQLdb 在使用方式上是类似的: pymysql.Connect()参数说明 host(str): MySQL服务器地址 port(int): MySQL服务器端口号 user(str): 用户名 passw

xtrabackup 备份mysql数据库三: innobackupex 测试一个全量和两个增量的备份恢复测试

## 查看当前库中表的数据 ([email protected]) [test]>select count(*) from t_innodb; +----------+ | count(*) | +----------+ |        0 | +----------+ 1 row in set (0.00 sec) ## 执行插入数据操作,该操作在全备之后执行完成 ([email protected]) [test]>call addTest(100000,0); ## 执行全库备份 #

MySQL学习笔记_13_Linux下C++/C连接MySQL数据库(三) --处理返回数据

 Linux下C++/C连接MySQL数据库(三) --处理返回数据 一.通过返回结果集中的字段数 [cpp] view plaincopyprint? unsigned int mysql_field_count(MYSQL * connection); //将MYSQL_ROW的值作为一个存储了一行数据的数组... unsigned int mysql_field_count(MYSQL * connection); //将MYSQL_ROW的值作为一个存储了一行数据的数组... 示例:

Python操作mysql数据库出现pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check

今天在用Python操作mysql数据库出现pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check报错 "SELECT Failure_code,describe from failure_occur_now order by ID DESC LIMIT 1“黄色区域为报错的位置仔细查找,发现没有语法错误啊,后面将,describe删掉不报错了,原来describe应该是Mysq

MySQl 数据库 之 python模块 pymysql 简单介绍

终端输入指令: pip3 install pymysql 如果失败就检查 pip 指令的环境变量是否配置; 若不会,则自行百度; 等待下载完成后, 在终端直接进入 python 环境, 导入该模块, 若无报错,则证明下载成功.(如下图所示) 二. 开始使用: 1, 在py文件中导入该模块: import pymysql 2, 连接数据库服务器: conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password=''

mysql索引与pymysql模块

一.索引 1.索引介绍 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到复杂的查询操作,通过索引可以加速查询. 索引在MySQL中也叫做"键",是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索引是应用程序设计和开发的一个重要方面.若索引太多,应用程序的性能可能会受到影响.而索引太少,对查询性能又会产生影响,要找到一个平衡点,这对应用程序的性能